-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStudentDeleteGUI.java
More file actions
70 lines (58 loc) · 2.18 KB
/
Copy pathStudentDeleteGUI.java
File metadata and controls
70 lines (58 loc) · 2.18 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
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class StudentDeleteGUI {
public static void main(String[] args) {
Frame frame = new Frame("Delete Student");
frame.setSize(350, 200);
frame.setLayout(null);
// Close window properly
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
frame.dispose();
}
});
Label idLabel = new Label("Enter Student ID:");
idLabel.setBounds(30, 30, 120, 25);
TextField idField = new TextField();
idField.setBounds(160, 30, 150, 25);
Button deleteButton = new Button("Delete");
deleteButton.setBounds(100, 80, 120, 30);
Label statusLabel = new Label("");
statusLabel.setBounds(30, 120, 280, 25);
frame.add(idLabel);
frame.add(idField);
frame.add(deleteButton);
frame.add(statusLabel);
deleteButton.addActionListener(e -> {
int id;
try {
id = Integer.parseInt(idField.getText());
} catch (NumberFormatException ex) {
statusLabel.setText("Invalid ID format!");
return;
}
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/studentsdb?useSSL=false&serverTimezone=UTC",
"root", ""
);
String sql = "DELETE FROM students WHERE id = ?";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setInt(1, id);
int rowsDeleted = stmt.executeUpdate();
if (rowsDeleted > 0) {
statusLabel.setText("Student deleted successfully!");
} else {
statusLabel.setText("Student ID not found.");
}
conn.close();
} catch (Exception ex) {
ex.printStackTrace();
statusLabel.setText("Error deleting student.");
}
});
frame.setVisible(true);
}
}