-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStudentUpdateGUI.java
More file actions
91 lines (76 loc) · 3.06 KB
/
Copy pathStudentUpdateGUI.java
File metadata and controls
91 lines (76 loc) · 3.06 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
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class StudentUpdateGUI {
public static void main(String[] args) {
Frame frame = new Frame("Update Student");
frame.setSize(400, 300);
frame.setLayout(null);
// Close window properly
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
frame.dispose();
}
});
Label idLabel = new Label("Student ID:");
idLabel.setBounds(30, 30, 100, 25);
TextField idField = new TextField();
idField.setBounds(140, 30, 200, 25);
Label nameLabel = new Label("New Name:");
nameLabel.setBounds(30, 70, 100, 25);
TextField nameField = new TextField();
nameField.setBounds(140, 70, 200, 25);
Label emailLabel = new Label("New Email:");
emailLabel.setBounds(30, 110, 100, 25);
TextField emailField = new TextField();
emailField.setBounds(140, 110, 200, 25);
Label courseLabel = new Label("New Course:");
courseLabel.setBounds(30, 150, 100, 25);
TextField courseField = new TextField();
courseField.setBounds(140, 150, 200, 25);
Button updateButton = new Button("Update");
updateButton.setBounds(130, 190, 120, 30);
Label statusLabel = new Label("");
statusLabel.setBounds(30, 230, 350, 25);
frame.add(idLabel);
frame.add(idField);
frame.add(nameLabel);
frame.add(nameField);
frame.add(emailLabel);
frame.add(emailField);
frame.add(courseLabel);
frame.add(courseField);
frame.add(updateButton);
frame.add(statusLabel);
updateButton.addActionListener(e -> {
try {
int id = Integer.parseInt(idField.getText());
String name = nameField.getText();
String email = emailField.getText();
String course = courseField.getText();
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/studentsdb?useSSL=false&serverTimezone=UTC",
"root", ""
);
String sql = "UPDATE students SET name = ?, email = ?, course = ? WHERE id = ?";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, name);
stmt.setString(2, email);
stmt.setString(3, course);
stmt.setInt(4, id);
int rowsUpdated = stmt.executeUpdate();
if (rowsUpdated > 0) {
statusLabel.setText("Student updated successfully!");
} else {
statusLabel.setText("Student ID not found.");
}
conn.close();
} catch (Exception ex) {
ex.printStackTrace();
statusLabel.setText("Error updating student.");
}
});
frame.setVisible(true);
}
}