-
Notifications
You must be signed in to change notification settings - Fork 3
/
StudentDetails.java
311 lines (288 loc) · 9.97 KB
/
StudentDetails.java
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package university.management.system;
//
//import javax.swing.*;
//import javax.swing.table.DefaultTableModel;
//import javax.swing.table.AbstractTableModel;
//import java.awt.*;
//import java.sql.*;
////import net.proteanit.sql.DbUtils;
//import java.awt.event.*;
//import javax.swing.plaf.OptionPaneUI;
//import javax.swing.table.DefaultTableModel;
//import javax.swing.table.TableColumnModel;
//
//public class StudentDetails extends JFrame implements ActionListener {
//
// Choice crollno;
// JTable table;
// JButton search, print, update, add, cancel;
//
// StudentDetails() {
//
// getContentPane().setBackground(Color.WHITE);
// setLayout(null);
// setResizable(false);
// JLabel heading = new JLabel("Search by Roll Number");
// heading.setBounds(20, 20, 150, 20);
// add(heading);
//
// crollno = new Choice();
// crollno.setBounds(180, 20, 150, 20);
// add(crollno);
// String query ="select * from student";
// try {
// Conn c = new Conn();
// ResultSet rs = c.s.executeQuery(query);
// while(rs.next()) {
// crollno.add(rs.getString("rollno"));
// }
// } catch (Exception e) {
// e.printStackTrace();
// }
//
// table = new JTable();
//
// try {
// Conn c = new Conn();
// ResultSet rs = c.s.executeQuery("select * from student");
// ResultSetMetaData metaData = rs.getMetaData();
// int columnCount = metaData.getColumnCount();
// String[] columnNames = new String[columnCount];
// for (int i = 1; i <= columnCount; i++) {
// columnNames[i - 1] = metaData.getColumnName(i);
// }
//
// DefaultTableModel model = new DefaultTableModel(columnNames, 0);
//
// while (rs.next()) {
// String[] rowData = new String[columnCount];
// for (int i = 1; i <= columnCount; i++) {
// rowData[i - 1] = rs.getString(i); // Adjust getString for other data types
// }
// model.addRow(rowData);
// }
//
//// table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
//// table.setRowMargin(5);
// table.setBackground(Color.WHITE);
// table.setForeground(Color.BLACK);
// table.setGridColor(Color.lightGray);
// table.setSelectionBackground(new Color(144230144));
// table.setSelectionForeground(Color.BLACK);
// table.setRowMargin(1);
// table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
//// table.setGridColor(Color.darkGray);
//// table.setForeground(Color.BLACK);
// table.setFont(new Font("TimesNewRoman",Font.BOLD,14));
//// table.setSelectionForeground(Color.MAGENTA);
//// table.setSelectionBackground(Color.lightGray);
//
// table.setModel(model);
//
//
//// table.setModel(DbUtils.resultSetToTableModel(rs));
// } catch (Exception e) {
// e.printStackTrace();
// }
//
// JScrollPane jsp = new JScrollPane(table);
// jsp.setBounds(0, 100, 1300, 600);
// add(jsp);
//
// search = new JButton("Search");
// search.setBounds(20, 70, 80, 20);
// search.addActionListener(this);
// add(search);
//
// print = new JButton("Print");
// print.setBounds(120, 70, 80, 20);
// print.addActionListener(this);
// add(print);
//
// add = new JButton("Add");
// add.setBounds(220, 70, 80, 20);
// add.addActionListener(this);
// add(add);
//
// update = new JButton("Update");
// update.setBounds(320, 70, 80, 20);
// update.addActionListener(this);
// add(update);
//
// cancel = new JButton("Cancel");
// cancel.setBounds(420, 70, 80, 20);
// cancel.addActionListener(this);
// add(cancel);
//
// setSize(1300, 700);
// setLocation(330, 100);
// setVisible(true);
// }
//
// public void actionPerformed(ActionEvent ae) {
// if (ae.getSource() == search) {
// String query = "select * from student where rollno = '"+crollno.getSelectedItem()+"'";
// try {
// Conn c = new Conn();
// ResultSet rs = c.s.executeQuery(query);
// ResultSetMetaData metaData = rs.getMetaData();
// int columnCount = metaData.getColumnCount();
// String[] columnNames = new String[columnCount];
// for (int i = 1; i <= columnCount; i++) {
// columnNames[i - 1] = metaData.getColumnName(i);
// }
//
// DefaultTableModel model = new DefaultTableModel(columnNames, 0);
//
// while (rs.next()) {
// String[] rowData = new String[columnCount];
// for (int i = 1; i <= columnCount; i++) {
// rowData[i - 1] = rs.getString(i); // Adjust getString for other data types
// }
// model.addRow(rowData);
// }
//
// table.setModel(model);
//// table.setModel(DbUtils.resultSetToTableModel(rs));
// } catch (Exception e) {
// e.printStackTrace();
// }
// } else if (ae.getSource() == print) {
// try {
// table.print();
// } catch (Exception e) {
// e.printStackTrace();
// }
// } else if (ae.getSource() == add) {
// setVisible(false);
// new AddStudent();
// } else if (ae.getSource() == update) {
// setVisible(false);
// new UpdateStudent();
// } else {
// setVisible(false);
// }
// }
//
// public static void main(String[] args) {
// new StudentDetails();
// }
//}
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.sql.*;
import java.awt.event.*;
import java.util.Vector;
public class StudentDetails extends JFrame implements ActionListener {
Choice crollno;
JTable table;
JButton search, print, update, add, cancel;
StudentDetails() {
getContentPane().setBackground(Color.WHITE);
setLayout(null);
setResizable(false);
JLabel heading = new JLabel("Search by Roll Number");
heading.setBounds(20, 20, 150, 20);
add(heading);
crollno = new Choice();
crollno.setBounds(180, 20, 150, 20);
add(crollno);
try {
Conn c = new Conn();
ResultSet rs = c.s.executeQuery("select * from student");
while(rs.next()) {
crollno.add(rs.getString("rollno"));
}
} catch (Exception e) {
e.printStackTrace();
}
table = new JTable();
populateTable();
JScrollPane jsp = new JScrollPane(table);
jsp.setBounds(0, 100, 1300, 600);
add(jsp);
search = new JButton("Search");
search.setBounds(20, 70, 80, 20);
search.addActionListener(this);
add(search);
print = new JButton("Print");
print.setBounds(120, 70, 80, 20);
print.addActionListener(this);
add(print);
add = new JButton("Add");
add.setBounds(220, 70, 80, 20);
add.addActionListener(this);
add(add);
update = new JButton("Update");
update.setBounds(320, 70, 80, 20);
update.addActionListener(this);
add(update);
cancel = new JButton("Cancel");
cancel.setBounds(420, 70, 80, 20);
cancel.addActionListener(this);
add(cancel);
setSize(1300, 700);
setLocation(330, 100);
setVisible(true);
}
private void populateTable() {
try {
Conn c = new Conn();
ResultSet rs = c.s.executeQuery("select * from student");
table.setModel(buildTableModel(rs));
} catch (Exception e) {
e.printStackTrace();
}
}
public static DefaultTableModel buildTableModel(ResultSet rs)
throws SQLException {
ResultSetMetaData metaData = rs.getMetaData();
// names of columns
Vector<String> columnNames = new Vector<String>();
int columnCount = metaData.getColumnCount();
for (int column = 1; column <= columnCount; column++) {
columnNames.add(metaData.getColumnName(column));
}
// data of the table
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
while (rs.next()) {
Vector<Object> vector = new Vector<Object>();
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
vector.add(rs.getObject(columnIndex));
}
data.add(vector);
}
return new DefaultTableModel(data, columnNames);
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == search) {
String query = "select * from student where rollno = '" + crollno.getSelectedItem() + "'";
try {
Conn c = new Conn();
ResultSet rs = c.s.executeQuery(query);
table.setModel(buildTableModel(rs));
} catch (Exception e) {
e.printStackTrace();
}
} else if (ae.getSource() == print) {
try {
table.print();
} catch (Exception e) {
e.printStackTrace();
}
} else if (ae.getSource() == add) {
setVisible(false);
new AddStudent().setVisible(true);
} else if (ae.getSource() == update) {
String selectedRollNo = crollno.getSelectedItem();
setVisible(false);
new UpdateStudent().setVisible(true);
} else {
setVisible(false);
}
}
public static void main(String[] args) {
new StudentDetails();
}
}