-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTeacherDetails.java
148 lines (124 loc) · 4.71 KB
/
TeacherDetails.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
package university.management.system;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.sql.*;
//import net.proteanit.sql.DbUtils;
import java.awt.event.*;
public class TeacherDetails extends JFrame implements ActionListener {
Choice cEmpId;
JTable table;
JButton search, print, update, add, cancel;
TeacherDetails() {
getContentPane().setBackground(Color.WHITE);
setLayout(null);
JLabel heading = new JLabel("Search by Employee Id");
heading.setBounds(20, 20, 150, 20);
add(heading);
cEmpId = new Choice();
cEmpId.setBounds(180, 20, 150, 20);
add(cEmpId);
try {
Conn c = new Conn();
ResultSet rs = c.s.executeQuery("select * from teacher");
while(rs.next()) {
cEmpId.add(rs.getString("empId"));
}
} catch (Exception e) {
e.printStackTrace();
}
table = new JTable();
try {
Conn c = new Conn();
ResultSet rs = c.s.executeQuery("select * from teacher");
// table.setModel(DbUtils.resultSetToTableModel(rs));
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);
} catch (Exception e) {
e.printStackTrace();
}
JScrollPane jsp = new JScrollPane(table);
jsp.setBounds(0, 100, 1100, 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(1100, 700);
setLocation(300, 100);
setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == search) {
String query = "select * from teacher where rollno = '"+cEmpId.getSelectedItem()+"'";
try {
Conn c = new Conn();
ResultSet rs = c.s.executeQuery(query);
// table.setModel(DbUtils.resultSetToTableModel(rs));
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);
} 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 AddTeacher();
} else if (ae.getSource() == update) {
setVisible(false);
new UpdateTeacher();
} else {
setVisible(false);
}
}
public static void main(String[] args) {
new TeacherDetails();
}
}