This repository was archived by the owner on Jun 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathterminator.java
More file actions
155 lines (130 loc) · 5.85 KB
/
terminator.java
File metadata and controls
155 lines (130 loc) · 5.85 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class terminator {
// JDBC connection details
private static final String JDBC_URL = "jdbc:mysql://localhost:3306/examdb";
private static final String USERNAME = "root";
private static final String PASSWORD = "Chintutyagi@12";
private JFrame frame;
private JButton assignSeatsButton;
private JButton clearDatabaseButton;
private JTextArea seatingTextArea;
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
try {
terminator window = new terminator();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
});
}
public terminator() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setTitle("Exam Seating Arrangement");
frame.setBounds(100, 100, 450, 350);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
assignSeatsButton = new JButton("Assign Seats");
assignSeatsButton.setBounds(30, 20, 120, 25);
frame.getContentPane().add(assignSeatsButton);
clearDatabaseButton = new JButton("Clear Database");
clearDatabaseButton.setBounds(160, 20, 120, 25);
frame.getContentPane().add(clearDatabaseButton);
seatingTextArea = new JTextArea();
seatingTextArea.setEditable(false);
seatingTextArea.setBounds(20, 60, 390, 220);
frame.getContentPane().add(seatingTextArea);
assignSeatsButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
assignSeats();
}
});
clearDatabaseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
clearDatabase();
}
});
}
private void assignSeats() {
try (Connection conn = DriverManager.getConnection(JDBC_URL, USERNAME, PASSWORD)) {
// Check if the 'seats' column exists in the table
DatabaseMetaData meta = conn.getMetaData();
ResultSet rs = meta.getColumns(null, null, "students", "seats");
boolean seatsColumnExists = rs.next();
rs.close();
// If the 'seats' column does not exist, add it to the table
if (!seatsColumnExists) {
String alterTableQuery = "ALTER TABLE students ADD COLUMN seats INT";
try (Statement stmt = conn.createStatement()) {
stmt.executeUpdate(alterTableQuery);
}
}
// Retrieve the student records
String selectQuery = "SELECT name, unique_id, exam FROM students";
try (Statement stmt = conn.createStatement();
ResultSet resultSet = stmt.executeQuery(selectQuery)) {
ArrayList<Integer> seatNumbers = new ArrayList<>();
for (int i = 1; i <= 100; i++) {
seatNumbers.add(i);
}
Collections.shuffle(seatNumbers);
String updateQuery = "UPDATE students SET seats = ? WHERE unique_id = ?";
try (PreparedStatement pstmt = conn.prepareStatement(updateQuery)) {
int index = 0;
while (resultSet.next()) {
String uniqueId = resultSet.getString("unique_id");
int seatNumber = seatNumbers.get(index);
pstmt.setInt(1, seatNumber);
pstmt.setString(2, uniqueId);
pstmt.executeUpdate();
index++;
}
}
}
// Retrieve the updated seating arrangement
String selectQueryWithSeats = "SELECT name, unique_id, exam, seats FROM students ORDER BY seats";
try (Statement stmt = conn.createStatement();
ResultSet resultSet = stmt.executeQuery(selectQueryWithSeats)) {
StringBuilder seatingArrangement = new StringBuilder();
seatingArrangement.append("Seating Arrangement:\n");
seatingArrangement.append("Name\t\t\t\tUnique ID\tExam\t\tSeat\n");
seatingArrangement.append("---------------------------------------------------\n");
while (resultSet.next()) {
String name = resultSet.getString("name");
String uniqueId = resultSet.getString("unique_id");
String exam = resultSet.getString("exam");
int seatNumber = resultSet.getInt("seats");
seatingArrangement.append(name);
seatingArrangement.append("\t\t");
seatingArrangement.append(uniqueId);
seatingArrangement.append("\t\t");
seatingArrangement.append(exam);
seatingArrangement.append("\t\t");
seatingArrangement.append(seatNumber);
seatingArrangement.append("\n");
}
seatingTextArea.setText(seatingArrangement.toString());
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
private void clearDatabase() {
try (Connection conn = DriverManager.getConnection(JDBC_URL, USERNAME, PASSWORD)) {
String clearQuery = "TRUNCATE TABLE students";
try (Statement stmt = conn.createStatement()) {
stmt.executeUpdate(clearQuery);
}
seatingTextArea.setText("Database cleared.");
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}