-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStudentInsertGUI.java
More file actions
113 lines (96 loc) · 3.97 KB
/
Copy pathStudentInsertGUI.java
File metadata and controls
113 lines (96 loc) · 3.97 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
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class StudentInsertGUI {
public static void main(String[] args) {
Frame frame = new Frame("Student Insert Form");
frame.setSize(400, 350);
frame.setLayout(null);
// Close operation
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
frame.dispose();
System.exit(0);
}
});
// Labels
Label idLabel = new Label("Student ID:");
Label nameLabel = new Label("Name:");
Label emailLabel = new Label("Email:");
Label courseLabel = new Label("Course:");
// Text fields
TextField idField = new TextField();
TextField nameField = new TextField();
TextField emailField = new TextField();
TextField courseField = new TextField();
// Button
Button submitButton = new Button("Insert");
// Set bounds (x, y, width, height)
idLabel.setBounds(50, 30, 100, 25);
idField.setBounds(150, 30, 180, 25);
nameLabel.setBounds(50, 70, 100, 25);
nameField.setBounds(150, 70, 180, 25);
emailLabel.setBounds(50, 110, 100, 25);
emailField.setBounds(150, 110, 180, 25);
courseLabel.setBounds(50, 150, 100, 25);
courseField.setBounds(150, 150, 180, 25);
submitButton.setBounds(130, 210, 100, 30);
// Add components
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(submitButton);
frame.setVisible(true);
// Button action
submitButton.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 = "INSERT INTO students (id, name, email, course) VALUES (?, ?, ?, ?)";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setInt(1, id);
stmt.setString(2, name);
stmt.setString(3, email);
stmt.setString(4, course);
int rowsInserted = stmt.executeUpdate();
if (rowsInserted > 0) {
// AWT MessageDialog alternative using Dialog
Dialog dialog = new Dialog(frame, "Success", true);
dialog.setLayout(new FlowLayout());
Label msg = new Label("Student inserted successfully!");
Button ok = new Button("OK");
ok.addActionListener(ev -> dialog.dispose());
dialog.add(msg);
dialog.add(ok);
dialog.setSize(250, 100);
dialog.setLocationRelativeTo(frame);
dialog.setVisible(true);
}
conn.close();
} catch (Exception ex) {
Dialog errorDialog = new Dialog(frame, "Error", true);
errorDialog.setLayout(new FlowLayout());
Label msg = new Label("Error: " + ex.getMessage());
Button ok = new Button("OK");
ok.addActionListener(ev -> errorDialog.dispose());
errorDialog.add(msg);
errorDialog.add(ok);
errorDialog.setSize(300, 100);
errorDialog.setLocationRelativeTo(frame);
errorDialog.setVisible(true);
}
});
}
}