-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFetchData.java
More file actions
177 lines (143 loc) · 5.25 KB
/
FetchData.java
File metadata and controls
177 lines (143 loc) · 5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package StudentDatabase.gitRepo.StudentDatabaseSystem;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;
import java.awt.*;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.*;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
public class FetchData extends JFrame implements ActionListener{
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int Screenwidth = (int) screenSize.getWidth();
int ScreenHeight = (int) screenSize.getHeight();
int width, height;
String rollNo;
String Name;
JLabel title, rollno, name;
JButton getByRollNo, getByName, back;
JPanel main;
JTextField byRollNo, byName;
ResultSet results;
boolean resultExists = false, getRollNumber = false;
public FetchData(Point loc, int w, int h) {
width = w;
height = h;
createFrame(loc);
}
private void createFrame(Point loc){
setLayout(null);
// comment following line before running main program
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
title = new JLabel("Fetch Student Information", SwingConstants.CENTER);
title.setBounds(50, 10, width - 100, 80);
title.setFont(new Font("", Font.PLAIN, 30));
add(title);
main = new JPanel();
main.setLayout(null);
main.setSize(590, 200);
main.setLocation(50, 140);
rollno = new JLabel("Roll No.");
rollno.setBounds(30, 25, 100, 30);
main.add(rollno);
byRollNo = new JTextField(20);
byRollNo.setBounds(140, 30, 200, 20);
main.add(byRollNo);
getByRollNo = new JButton("Search");
getByRollNo.setBounds(440, 25, 80, 30);
main.add(getByRollNo);
getByRollNo.addActionListener(this);
name = new JLabel("Name");
name.setBounds(30, 95, 100, 30);
main.add(name);
byName = new JTextField(20);
byName.setBounds(140, 100, 200, 20);
main.add(byName);
getByName = new JButton("Search");
getByName.setBounds(440, 95, 80, 30);
main.add(getByName);
getByName.addActionListener(this);
back = new JButton("Back");
back.setBounds(0, 400, 70, 30);
add(back);
back.addActionListener(this);
add(main);
setTitle("Student Database System");
setLocation(loc);
setSize(width, height);
setVisible(true);
}
private void fetchDataFromDatabase(String data){
try{
Class.forName("com.mysql.jdbc.Driver");
String link = "jdbc:mysql://localhost:3308/StudentDatabase?autoReconnect=true&useSSL=false";
String username = "root";
String password = "";
Connection con = DriverManager.getConnection(link, username, password);
String sql = "";
if(getRollNumber)
sql = "select * from student where RollNo = " + data;
else
sql = "select * from student where Name = '" + data + "'";
Statement stmt = con.createStatement();
results = stmt.executeQuery(sql);
if(!results.isBeforeFirst()){
if(getRollNumber){
JOptionPane.showMessageDialog(rootPane, "No student from Provided roll no.");
getRollNumber = false;
}
else
JOptionPane.showMessageDialog(rootPane, "No student from Provided Name");
return;
}
else{
resultExists = true;
}
}
catch(Exception e){
// System.out.println(e);
JOptionPane.showMessageDialog(rootPane, "Some error occurred while connecting to database");
}
}
public static void main(String args[]){
new FetchData(new Point(300, 100), 700, 600);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == back){
new Main();
dispose();
}
else if(e.getSource() == getByRollNo){
rollNo = byRollNo.getText();
getRollNumber = true;
fetchDataFromDatabase(rollNo);
getRollNumber = false;
try {
if(resultExists){
ShowFetchedData dataObj = new ShowFetchedData(getLocation(), results, getWidth(), getHeight());
dispose();
}
}
catch (SQLException ex) {
JOptionPane.showMessageDialog(rootPane, "Some error occurred");
}
}
else{
Name = byName.getText();
fetchDataFromDatabase(Name);
if(resultExists){
new ShowNames(getLocation(), getWidth(), getHeight(), results);
dispose();
}
}
}
}