-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStudentViewGUI.java
More file actions
66 lines (57 loc) · 2.34 KB
/
Copy pathStudentViewGUI.java
File metadata and controls
66 lines (57 loc) · 2.34 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
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class StudentViewGUI {
public static void main(String[] args) {
Frame frame = new Frame("View Students");
frame.setSize(600, 400);
frame.setLayout(new BorderLayout());
// Close operation
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
frame.dispose();
}
});
// Use a TextArea for multi-line text display (read-only)
TextArea textArea = new TextArea();
textArea.setEditable(false);
textArea.setFont(new Font("Monospaced", Font.PLAIN, 12));
// Header for columns - fixed width formatting
String header = String.format("%-6s %-20s %-30s %-15s%n", "ID", "Name", "Email", "Course");
textArea.append(header);
textArea.append("-------------------------------------------------------------\n");
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/studentsdb?useSSL=false&serverTimezone=UTC",
"root", ""
);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM students");
while (rs.next()) {
String row = String.format("%-6d %-20s %-30s %-15s%n",
rs.getInt("id"),
rs.getString("name"),
rs.getString("email"),
rs.getString("course"));
textArea.append(row);
}
conn.close();
} catch (Exception e) {
e.printStackTrace();
// Show an AWT dialog on error
Dialog errorDialog = new Dialog(frame, "Error", true);
errorDialog.setLayout(new FlowLayout());
Label msg = new Label("Error loading student data.");
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);
}
frame.add(textArea, BorderLayout.CENTER);
frame.setVisible(true);
}
}