-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathViewSalary.java
120 lines (101 loc) · 3.73 KB
/
ViewSalary.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
package employee.management.system;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import javax.swing.table.DefaultTableModel;
public class ViewSalary extends JFrame implements ActionListener {
JTable table;
Choice choiceEMP;
JButton searchbtn, print, back;
ViewSalary() {
getContentPane().setBackground(new Color(122, 140, 255));
setLayout(null);
JLabel search = new JLabel("Search by employee id");
search.setBounds(20, 20, 220, 20);
search.setFont(new Font("SERIF", Font.BOLD, 21));
add(search);
choiceEMP = new Choice();
choiceEMP.setBounds(270, 20, 150, 20);
add(choiceEMP);
try {
conn c = new conn();
ResultSet resultSet = c.statement.executeQuery("select * from employee");
while (resultSet.next()) {
choiceEMP.add(resultSet.getString("empId"));
}
} catch (Exception e) {
e.printStackTrace();
}
table = new JTable();
try {
conn c = new conn();
ResultSet resultSet = c.statement.executeQuery("select * from employee_salary");
table.setModel(resultSetToTableModel(resultSet));
} catch (Exception e) {
e.printStackTrace();
}
JScrollPane jp = new JScrollPane(table);
jp.setBounds(0, 100, 900, 600);
jp.setFont(new Font("SERIF", Font.BOLD, 15));
add(jp);
searchbtn = new JButton("Search");
searchbtn.setBounds(20, 70, 80, 20);
searchbtn.addActionListener(this);
add(searchbtn);
print = new JButton("Print");
print.setBounds(120, 70, 80, 20);
print.addActionListener(this);
add(print);
back = new JButton("Back");
back.setBounds(220, 70, 80, 20);
back.addActionListener(this);
add(back);
setSize(900, 700);
setLocation(300, 100);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == searchbtn) {
String query = "select * from employee_salary where emp_id = '" + choiceEMP.getSelectedItem() + "'";
try {
conn c = new conn();
ResultSet resultSet = c.statement.executeQuery(query);
table.setModel(resultSetToTableModel(resultSet));
} catch (Exception ex) {
ex.printStackTrace();
}
} else if (e.getSource() == print) {
try {
table.print();
} catch (Exception ex) {
ex.printStackTrace();
}
} else if (e.getSource() == back) {
setVisible(false);
new Main_class(); // Assuming Main_class is the class to return to main menu
}
}
public static void main(String[] args) {
new ViewSalary();
}
private DefaultTableModel resultSetToTableModel(ResultSet rs) throws Exception {
ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount();
DefaultTableModel model = new DefaultTableModel();
for (int i = 1; i <= columnCount; i++) {
model.addColumn(metaData.getColumnLabel(i));
}
while (rs.next()) {
Object[] row = new Object[columnCount];
for (int i = 1; i <= columnCount; i++) {
row[i - 1] = rs.getObject(i);
}
model.addRow(row);
}
return model;
}
}