-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManagerDashboard.java
More file actions
139 lines (126 loc) · 5.92 KB
/
Copy pathManagerDashboard.java
File metadata and controls
139 lines (126 loc) · 5.92 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
//Test comment
public class ManagerDashboard extends JFrame {
private final String dbUrl, dbUser, dbPassword;
public ManagerDashboard(String dbUrl, String dbUser, String dbPassword) {
this.dbUrl = dbUrl;
this.dbUser = dbUser;
this.dbPassword = dbPassword;
setTitle("Manager Dashboard");
setSize(400, 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton viewScheduleBtn = new JButton("View Student Schedule");
JButton viewRosterBtn = new JButton("View Class Roster");
JButton addStudentBtn = new JButton("Add Student to Class");
JButton dropStudentBtn = new JButton("Drop Student from Class");
JButton newStudentBtn = new JButton("Add New Student");
viewScheduleBtn.addActionListener(e -> viewStudentSchedule());
viewRosterBtn.addActionListener(e -> viewClassRoster());
addStudentBtn.addActionListener(e -> addStudentToClass());
dropStudentBtn.addActionListener(e -> dropStudentFromClass());
newStudentBtn.addActionListener(e -> addNewStudent());
setLayout(new GridLayout(5, 1, 10, 10));
add(viewScheduleBtn);
add(viewRosterBtn);
add(addStudentBtn);
add(dropStudentBtn);
add(newStudentBtn);
setVisible(true);
}
private void viewStudentSchedule() {
String studentId = JOptionPane.showInputDialog(this, "Enter Student ID:");
if (studentId != null) {
try (Connection conn = DriverManager.getConnection(dbUrl, dbUser, dbPassword)) {
String query = "SELECT class_name FROM schedule WHERE student_id = ?";
try (PreparedStatement stmt = conn.prepareStatement(query)) {
stmt.setString(1, studentId);
ResultSet rs = stmt.executeQuery();
StringBuilder sb = new StringBuilder("Schedule:\n");
while (rs.next()) {
sb.append(rs.getString("class_name")).append("\n");
}
JOptionPane.showMessageDialog(this, sb.toString());
}
} catch (SQLException ex) {
showError(ex);
}
}
}
private void viewClassRoster() {
String className = JOptionPane.showInputDialog(this, "Enter Class Name:");
if (className != null) {
try (Connection conn = DriverManager.getConnection(dbUrl, dbUser, dbPassword)) {
String query = "SELECT student_id FROM schedule WHERE class_name = ?";
try (PreparedStatement stmt = conn.prepareStatement(query)) {
stmt.setString(1, className);
ResultSet rs = stmt.executeQuery();
StringBuilder sb = new StringBuilder("Roster:\n");
while (rs.next()) {
sb.append("Student ID: ").append(rs.getString("student_id")).append("\n");
}
JOptionPane.showMessageDialog(this, sb.toString());
}
} catch (SQLException ex) {
showError(ex);
}
}
}
private void addStudentToClass() {
String studentId = JOptionPane.showInputDialog(this, "Enter Student ID:");
String className = JOptionPane.showInputDialog(this, "Enter Class Name:");
if (studentId != null && className != null) {
try (Connection conn = DriverManager.getConnection(dbUrl, dbUser, dbPassword)) {
String query = "INSERT INTO schedule (student_id, class_name) VALUES (?, ?)";
try (PreparedStatement stmt = conn.prepareStatement(query)) {
stmt.setString(1, studentId);
stmt.setString(2, className);
stmt.executeUpdate();
JOptionPane.showMessageDialog(this, "Student added to class.");
}
} catch (SQLException ex) {
showError(ex);
}
}
}
private void dropStudentFromClass() {
String studentId = JOptionPane.showInputDialog(this, "Enter Student ID:");
String className = JOptionPane.showInputDialog(this, "Enter Class Name:");
if (studentId != null && className != null) {
try (Connection conn = DriverManager.getConnection(dbUrl, dbUser, dbPassword)) {
String query = "DELETE FROM schedule WHERE student_id = ? AND class_name = ?";
try (PreparedStatement stmt = conn.prepareStatement(query)) {
stmt.setString(1, studentId);
stmt.setString(2, className);
stmt.executeUpdate();
JOptionPane.showMessageDialog(this, "Student removed from class.");
}
} catch (SQLException ex) {
showError(ex);
}
}
}
private void addNewStudent() {
String studentId = JOptionPane.showInputDialog(this, "Enter New Student ID:");
String name = JOptionPane.showInputDialog(this, "Enter Student Name:");
if (studentId != null && name != null) {
try (Connection conn = DriverManager.getConnection(dbUrl, dbUser, dbPassword)) {
String query = "INSERT INTO students (student_id, name) VALUES (?, ?)";
try (PreparedStatement stmt = conn.prepareStatement(query)) {
stmt.setString(1, studentId);
stmt.setString(2, name);
stmt.executeUpdate();
JOptionPane.showMessageDialog(this, "Student added.");
}
} catch (SQLException ex) {
showError(ex);
}
}
}
private void showError(Exception ex) {
JOptionPane.showMessageDialog(this, "Error: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}