-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCurdGUI.java
More file actions
320 lines (257 loc) · 10.6 KB
/
Copy pathCurdGUI.java
File metadata and controls
320 lines (257 loc) · 10.6 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
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
312
313
314
315
316
317
318
319
320
package crud;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class CrudGUI {
static Connection con;
static JTable table;
static DefaultTableModel model;
static JTextField t1, t2, t3, t4, t5, t6, searchField;
static JComboBox<String> genderBox;
static TableRowSorter<DefaultTableModel> sorter;
// LIGHT THEME COLORS
static Color bg = new Color(245, 246, 250);
static Color card = new Color(255, 255, 255);
static Color accent = new Color(0, 120, 215);
static Color danger = new Color(220, 70, 70);
static Color success = new Color(46, 170, 110);
static Color text = new Color(30, 30, 30);
// ================= STYLE HELPERS =================
public static void styleButton(JButton b, Color c) {
b.setBackground(c);
b.setForeground(Color.WHITE);
b.setFocusPainted(false);
b.setFont(new Font("Segoe UI", Font.BOLD, 13));
b.setBorder(BorderFactory.createEmptyBorder(8, 14, 8, 14));
}
public static void styleField(JTextField t) {
t.setBackground(Color.WHITE);
t.setForeground(text);
t.setCaretColor(text);
t.setFont(new Font("Segoe UI", Font.PLAIN, 13));
t.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(new Color(200, 200, 200)),
BorderFactory.createEmptyBorder(6, 8, 6, 8)
));
}
// ================= CONNECT =================
public static void connect() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/school",
"root",
""
);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
// ================= LOAD TABLE =================
public static void loadTable() {
try {
model.setRowCount(0);
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM childd");
while (rs.next()) {
model.addRow(new Object[]{
rs.getInt("Cid"),
rs.getString("Fname"),
rs.getString("Lname"),
rs.getInt("Age"),
rs.getString("grade"),
rs.getString("s_name"),
rs.getString("gender")
});
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
public static void clearFields() {
t1.setText("");
t2.setText("");
t3.setText("");
t4.setText("");
t5.setText("");
t6.setText("");
genderBox.setSelectedIndex(0);
}
// ================= MAIN =================
public static void main(String[] args) {
connect();
JFrame f = new JFrame("Student Management System");
f.setSize(1200, 760);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel main = new JPanel(new BorderLayout(12, 12));
main.setBackground(bg);
main.setBorder(new EmptyBorder(15, 15, 15, 15));
// ================= TITLE =================
JLabel title = new JLabel("STUDENT MANAGEMENT SYSTEM", SwingConstants.CENTER);
title.setFont(new Font("Segoe UI", Font.BOLD, 26));
title.setForeground(text);
main.add(title, BorderLayout.NORTH);
// ================= SEARCH =================
searchField = new JTextField(20);
styleField(searchField);
JPanel searchPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
searchPanel.setBackground(bg);
searchPanel.add(new JLabel("Search: "));
searchPanel.add(searchField);
// ================= TABLE =================
model = new DefaultTableModel();
model.setColumnIdentifiers(new String[]{
"ID", "First Name", "Last Name", "Age", "Grade", "School", "Gender"
});
table = new JTable(model);
sorter = new TableRowSorter<>(model);
table.setRowSorter(sorter);
table.setRowHeight(26);
table.setFont(new Font("Segoe UI", Font.PLAIN, 13));
table.setSelectionBackground(new Color(220, 235, 255));
table.getTableHeader().setBackground(new Color(235, 235, 235));
table.getTableHeader().setFont(new Font("Segoe UI", Font.BOLD, 13));
JScrollPane sp = new JScrollPane(table);
JPanel tablePanel = new JPanel(new BorderLayout(10, 10));
tablePanel.setBackground(card);
tablePanel.setBorder(new EmptyBorder(10, 10, 10, 10));
tablePanel.add(searchPanel, BorderLayout.NORTH);
tablePanel.add(sp, BorderLayout.CENTER);
// ================= FORM =================
JPanel form = new JPanel(new GridLayout(7, 2, 10, 10));
form.setBackground(card);
form.setBorder(new EmptyBorder(15, 15, 15, 15));
t1 = new JTextField();
t2 = new JTextField();
t3 = new JTextField();
t4 = new JTextField();
t5 = new JTextField();
t6 = new JTextField();
genderBox = new JComboBox<>(new String[]{"Male", "Female"});
styleField(t1);
styleField(t2);
styleField(t3);
styleField(t4);
styleField(t5);
styleField(t6);
form.add(new JLabel("ID")); form.add(t1);
form.add(new JLabel("First Name")); form.add(t2);
form.add(new JLabel("Last Name")); form.add(t3);
form.add(new JLabel("Age")); form.add(t4);
form.add(new JLabel("Grade")); form.add(t5);
form.add(new JLabel("School")); form.add(t6);
form.add(new JLabel("Gender")); form.add(genderBox);
// ================= BUTTONS =================
JPanel buttons = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 10));
buttons.setBackground(bg);
JButton b1 = new JButton("Insert");
JButton b2 = new JButton("Update");
JButton b3 = new JButton("Delete");
JButton b4 = new JButton("Refresh");
JButton b5 = new JButton("Clear");
styleButton(b1, success);
styleButton(b2, accent);
styleButton(b3, danger);
styleButton(b4, new Color(120, 120, 120));
styleButton(b5, new Color(150, 150, 150));
buttons.add(b1);
buttons.add(b2);
buttons.add(b3);
buttons.add(b4);
buttons.add(b5);
JPanel bottom = new JPanel(new BorderLayout());
bottom.setBackground(bg);
bottom.add(form, BorderLayout.CENTER);
bottom.add(buttons, BorderLayout.SOUTH);
// ================= CENTER LAYOUT =================
JPanel center = new JPanel(new BorderLayout(10, 10));
center.setBackground(bg);
center.add(tablePanel, BorderLayout.CENTER);
center.add(bottom, BorderLayout.SOUTH);
main.add(center, BorderLayout.CENTER);
f.setContentPane(main);
f.setVisible(true);
loadTable();
// ================= SEARCH =================
searchField.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent e) {
sorter.setRowFilter(RowFilter.regexFilter("(?i)" + searchField.getText()));
}
});
// ================= INSERT =================
b1.addActionListener(e -> {
try {
PreparedStatement ps = con.prepareStatement(
"INSERT INTO childd VALUES (?, ?, ?, ?, ?, ?, ?)"
);
ps.setInt(1, Integer.parseInt(t1.getText()));
ps.setString(2, t2.getText());
ps.setString(3, t3.getText());
ps.setInt(4, Integer.parseInt(t4.getText()));
ps.setString(5, t5.getText());
ps.setString(6, t6.getText());
ps.setString(7, genderBox.getSelectedItem().toString());
ps.executeUpdate();
loadTable();
clearFields();
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex);
}
});
// ================= UPDATE =================
b2.addActionListener(e -> {
try {
PreparedStatement ps = con.prepareStatement(
"UPDATE childd SET Fname=?, Lname=?, Age=?, grade=?, s_name=?, gender=? WHERE Cid=?"
);
ps.setString(1, t2.getText());
ps.setString(2, t3.getText());
ps.setInt(3, Integer.parseInt(t4.getText()));
ps.setString(4, t5.getText());
ps.setString(5, t6.getText());
ps.setString(6, genderBox.getSelectedItem().toString());
ps.setInt(7, Integer.parseInt(t1.getText()));
ps.executeUpdate();
loadTable();
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex);
}
});
// ================= DELETE =================
b3.addActionListener(e -> {
try {
PreparedStatement ps = con.prepareStatement(
"DELETE FROM childd WHERE Cid=?"
);
ps.setInt(1, Integer.parseInt(t1.getText()));
ps.executeUpdate();
loadTable();
clearFields();
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex);
}
});
// ================= REFRESH =================
b4.addActionListener(e -> loadTable());
// ================= CLEAR =================
b5.addActionListener(e -> clearFields());
// ================= TABLE CLICK =================
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int r = table.getSelectedRow();
t1.setText(model.getValueAt(r, 0).toString());
t2.setText(model.getValueAt(r, 1).toString());
t3.setText(model.getValueAt(r, 2).toString());
t4.setText(model.getValueAt(r, 3).toString());
t5.setText(model.getValueAt(r, 4).toString());
t6.setText(model.getValueAt(r, 5).toString());
genderBox.setSelectedItem(model.getValueAt(r, 6).toString());
}
});
}
}