-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram7.java
More file actions
244 lines (214 loc) · 8.76 KB
/
Program7.java
File metadata and controls
244 lines (214 loc) · 8.76 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
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
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.sql.*;
import java.awt.event.ActionEvent;
public class Program7 extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField id;
private JTextField name;
private JTextField mobileNumber;
private JTextField searchId;
private JTable table;
private DefaultTableModel tableModel;
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
try {
Program7 frame = new Program7();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
});
}
public Program7() {
setTitle("Employee Management System by 22EARACS130");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 800, 500);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel Title = new JLabel("Emp Management By Niraj");
Title.setHorizontalAlignment(SwingConstants.CENTER);
Title.setForeground(new Color(0, 0, 255));
Title.setFont(new Font("Tahoma", Font.BOLD, 20));
Title.setBounds(241, 27, 303, 32);
contentPane.add(Title);
JPanel panel = new JPanel();
panel.setBackground(Color.WHITE);
panel.setBorder(new TitledBorder(new EtchedBorder(), "Enter Employee Details", TitledBorder.CENTER, TitledBorder.TOP, null, Color.RED));
panel.setBounds(47, 98, 286, 251);
contentPane.add(panel);
panel.setLayout(null);
JLabel lblId = new JLabel("Employee ID:");
lblId.setFont(new Font("Times New Roman", Font.BOLD, 11));
lblId.setBounds(10, 48, 82, 30);
panel.add(lblId);
id = new JTextField();
id.setBounds(124, 54, 126, 19);
panel.add(id);
id.setColumns(10);
JLabel lblName = new JLabel("Name:");
lblName.setFont(new Font("Times New Roman", Font.BOLD, 11));
lblName.setBounds(10, 88, 82, 30);
panel.add(lblName);
name = new JTextField();
name.setBounds(124, 94, 126, 19);
panel.add(name);
name.setColumns(10);
JLabel lblMobile = new JLabel("Mobile No:");
lblMobile.setFont(new Font("Times New Roman", Font.BOLD, 11));
lblMobile.setBounds(10, 128, 82, 30);
panel.add(lblMobile);
mobileNumber = new JTextField();
mobileNumber.setBounds(124, 134, 126, 19);
panel.add(mobileNumber);
mobileNumber.setColumns(10);
JButton btnSave = new JButton("Save");
btnSave.setBounds(100, 180, 85, 21);
panel.add(btnSave);
JPanel panel2 = new JPanel();
panel2.setForeground(Color.CYAN);
panel2.setBorder(new TitledBorder(new EtchedBorder(), "Employee Database", TitledBorder.CENTER, TitledBorder.TOP, null, Color.RED));
panel2.setBounds(423, 98, 286, 251);
contentPane.add(panel2);
panel2.setLayout(new BorderLayout());
tableModel = new DefaultTableModel(new String[]{"Series No", "Name", "ID", "Mobile No"}, 0);
table = new JTable(tableModel);
JScrollPane scrollPane = new JScrollPane(table);
panel2.add(scrollPane);
JLabel lblSearch = new JLabel("Enter ID:");
lblSearch.setFont(new Font("Times New Roman", Font.PLAIN, 11));
lblSearch.setBounds(47, 359, 58, 30);
contentPane.add(lblSearch);
searchId = new JTextField();
searchId.setBounds(132, 365, 96, 19);
contentPane.add(searchId);
searchId.setColumns(10);
JButton btnSearch = new JButton("Search");
btnSearch.setBounds(47, 397, 85, 21);
contentPane.add(btnSearch);
JButton btnUpdate = new JButton("Update");
btnUpdate.setBounds(231, 397, 85, 21);
contentPane.add(btnUpdate);
JButton btnDelete = new JButton("Delete");
btnDelete.setBounds(423, 397, 85, 21);
contentPane.add(btnDelete);
JButton btnLoad = new JButton("Load");
btnLoad.setBounds(624, 397, 85, 21);
contentPane.add(btnLoad);
btnSave.addActionListener(e -> saveEmployee());
btnSearch.addActionListener(e -> searchEmployee());
btnUpdate.addActionListener(e -> updateEmployee());
btnDelete.addActionListener(e -> deleteEmployee());
btnLoad.addActionListener(e -> loadEmployees());
}
private void saveEmployee() {
String eid = id.getText();
String ename = name.getText();
String emobile = mobileNumber.getText();
try (Connection con = connectToDatabase()) {
String query1 = "SELECT * FROM employee WHERE id = ?";
PreparedStatement pst1 = con.prepareStatement(query1);
pst1.setString(1, eid);
ResultSet rs = pst1.executeQuery();
if(rs.next())
{
JOptionPane.showMessageDialog(this, "Employee already exits!");
return;
}
String query = "INSERT INTO employee(name, id, mobile_number) VALUES (?, ?, ?)";
PreparedStatement pst = con.prepareStatement(query);
pst.setString(1, ename);
pst.setString(2, eid);
pst.setString(3, emobile);
pst.executeUpdate();
JOptionPane.showMessageDialog(this, "Employee Saved Successfully!");
clearFields();
loadEmployees();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void searchEmployee() {
String eid = searchId.getText();
try (Connection con = connectToDatabase()) {
String query = "SELECT * FROM employee WHERE id = ?";
PreparedStatement pst = con.prepareStatement(query);
pst.setString(1, eid);
ResultSet rs = pst.executeQuery();
if (rs.next()) {
tableModel.setRowCount(0);
tableModel.addRow(new Object[]{rs.getInt("series_number"), rs.getString("name"), rs.getString("id"), rs.getString("mobile_number")});
} else {
JOptionPane.showMessageDialog(this, "Employee Not Found!");
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void updateEmployee() {
String eid = id.getText();
String ename = name.getText();
String emobile = mobileNumber.getText();
try (Connection con = connectToDatabase()) {
String query = "UPDATE employee SET name = ?, mobile_number = ? WHERE id = ?";
PreparedStatement pst = con.prepareStatement(query);
pst.setString(1, ename);
pst.setString(2, emobile);
pst.setString(3, eid);
pst.executeUpdate();
JOptionPane.showMessageDialog(this, "Employee Updated Successfully!");
clearFields();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void deleteEmployee() {
String eid = searchId.getText();
if(eid == null)
{
JOptionPane.showMessageDialog(this, "Please enter a id ");
return;
}
try (Connection con = connectToDatabase()) {
String query = "DELETE FROM employee WHERE id = ?";
PreparedStatement pst = con.prepareStatement(query);
pst.setString(1, eid);
pst.executeUpdate();
loadEmployees();
JOptionPane.showMessageDialog(this, "Employee Deleted Successfully!");
clearFields();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void loadEmployees() {
try (Connection con = connectToDatabase()) {
String query = "SELECT * FROM employee";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
tableModel.setRowCount(0);
while (rs.next()) {
tableModel.addRow(new Object[]{rs.getInt("series_number"), rs.getString("name"), rs.getString("id"), rs.getString("mobile_number")});
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void clearFields() {
id.setText("");
name.setText("");
mobileNumber.setText("");
searchId.setText("");
id.requestFocus();
}
private Connection connectToDatabase() throws SQLException {
return DriverManager.getConnection("jdbc:mysql://localhost:3306/logindatabases","root","might100");
}
}