Skip to content
This repository was archived by the owner on Jun 18, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions AddClassGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private void initialize() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);

JLabel classLabel = new JLabel("Class:");
JLabel classLabel = new JLabel("Number:");
classLabel.setBounds(50, 50, 80, 25);
frame.getContentPane().add(classLabel);

Expand All @@ -55,12 +55,7 @@ private void initialize() {
frame.getContentPane().add(slotsComboBox);

// Add the slots to the combo box
String[] slots = {
"A1", "A2", "A3", "A4", "B1", "B2", "B3", "B4",
"C1", "C2", "C3", "C4", "D1", "D2", "D3", "D4",
"E1", "E2", "E3", "E4", "F1", "F2", "F3", "F4",
"G1", "G2", "G3", "G4"
};
String[] slots = {"A1", "A2", "B1", "B2", "C1", "C2"};
slotsComboBox.setModel(new DefaultComboBoxModel<>(slots));

addButton = new JButton("Add");
Expand All @@ -78,7 +73,7 @@ public void actionPerformed(ActionEvent e) {
String slot = (String) slotsComboBox.getSelectedItem();

if (className.isEmpty() || slot.isEmpty()) {
statusLabel.setText("Please enter class and select a slot.");
statusLabel.setText("Please enter a number and select a slot.");
} else {
addClass(className, slot);
}
Expand All @@ -88,14 +83,15 @@ public void actionPerformed(ActionEvent e) {

private void addClass(String className, String slot) {
try (Connection conn = DriverManager.getConnection(JDBC_URL, USERNAME, PASSWORD)) {
String insertQuery = "INSERT INTO classes (class, slot) VALUES (?, ?)";
try (PreparedStatement stmt = conn.prepareStatement(insertQuery)) {
String insertQuery = "INSERT INTO classes (class, slot) VALUES (?, ?) ON DUPLICATE KEY UPDATE class = ?";
try (PreparedStatement stmt = conn.prepareStatement(insertQuery, Statement.RETURN_GENERATED_KEYS)) {
stmt.setString(1, className);
stmt.setString(2, slot);
stmt.setString(3, className);
int rowsAffected = stmt.executeUpdate();

if (rowsAffected > 0) {
statusLabel.setText("Class added successfully.");
statusLabel.setText("Class added successfully. Slot: " + slot + ", Number: " + className);
classField.setText("");
slotsComboBox.setSelectedIndex(0);
} else {
Expand Down
58 changes: 32 additions & 26 deletions ExamSeatingArrangementGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class ExamSeatingArrangementGUI {
private JFrame frame;
private JTextField nameField;
private JTextField idField;
private JButton findSeatButton;
private JButton displayButton;
private JTextArea seatingTextArea;
private JLabel errorLabel;

Expand All @@ -33,7 +33,7 @@ public ExamSeatingArrangementGUI() {

private void initialize() {
frame = new JFrame();
frame.setTitle("Exam Seating Arrangement");
frame.setTitle("Exam Seating Display");
frame.setBounds(100, 100, 450, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
Expand All @@ -56,9 +56,9 @@ private void initialize() {
frame.getContentPane().add(idField);
idField.setColumns(10);

findSeatButton = new JButton("Find My Seat");
findSeatButton.setBounds(140, 130, 120, 25);
frame.getContentPane().add(findSeatButton);
displayButton = new JButton("Display");
displayButton.setBounds(140, 130, 120, 25);
frame.getContentPane().add(displayButton);

seatingTextArea = new JTextArea();
seatingTextArea.setEditable(false);
Expand All @@ -70,7 +70,7 @@ private void initialize() {
errorLabel.setForeground(Color.RED);
frame.getContentPane().add(errorLabel);

findSeatButton.addActionListener(new ActionListener() {
displayButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = nameField.getText();
String id = idField.getText();
Expand All @@ -79,38 +79,44 @@ public void actionPerformed(ActionEvent e) {
errorLabel.setText("Please enter name and ID.");
seatingTextArea.setText("");
} else {
findSeat(name, id);
displaySeatInfo(name, id);
}
}
});
}

private void findSeat(String name, String id) {
private void displaySeatInfo(String name, String id) {
try (Connection conn = DriverManager.getConnection(JDBC_URL, USERNAME, PASSWORD)) {
String selectQuery = "SELECT seats FROM students WHERE name = ? AND unique_id = ?";
try (PreparedStatement stmt = conn.prepareStatement(selectQuery)) {
String query = "SELECT s.slot_number, c.class, s.seats " +
"FROM students s " +
"JOIN classes c ON s.slot_number = c.slot " +
"WHERE s.name = ? AND s.unique_id = ?";
try (PreparedStatement stmt = conn.prepareStatement(query)) {
stmt.setString(1, name);
stmt.setString(2, id);

ResultSet rs = stmt.executeQuery();
if (rs.next()) {
int seats = rs.getInt("seats");

seatingTextArea.setText("Name: " + name + "\n" +
"Unique ID: " + id + "\n" +
"Seat Number: " + seats);
errorLabel.setText("");
} else {
seatingTextArea.setText("");
errorLabel.setText("Invalid name or ID. Please try again.");

try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
String slot = rs.getString("slot_number");
String studentClass = rs.getString("class");
int seats = rs.getInt("seats");

String seatingInfo = "Name: " + name + "\n" +
"Unique ID: " + id + "\n" +
"Slot: " + slot + "\n" +
"Class: " + studentClass + "\n" +
"Seats: " + seats;

seatingTextArea.setText(seatingInfo);
errorLabel.setText("");
} else {
seatingTextArea.setText("");
errorLabel.setText("No seat found for the given name and ID.");
}
}
}
} catch (SQLException ex) {
ex.printStackTrace();
errorLabel.setText("Error connecting to the database.");
seatingTextArea.setText("");
}
}


}
17 changes: 14 additions & 3 deletions jdbc.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ private static void createTable(Connection conn) throws SQLException {
}

private static void generateStudentData(Connection conn) throws SQLException {
String[] slots = {"A1", "A1", "A2", "A2", "A3", "A3", "A4", "A4", "A5", "A5", "A6", "A6"};
String[] names = {"John Doe", "Jane Smith", "Mike Johnson", "Emily Davis", "Robert Wilson", "Olivia Brown",
"David Taylor", "Sophia Miller", "Daniel Anderson", "Ava Garcia", "William Thomas", "Mia Martinez"};
String[] uniqueIds = {"001", "002", "003", "004", "005", "006", "007", "008", "009", "010", "011", "012"};
Expand All @@ -48,18 +47,30 @@ private static void generateStudentData(Connection conn) throws SQLException {

try (PreparedStatement stmt = conn.prepareStatement(insertQuery)) {
for (int i = 0; i < names.length; i++) {
String slot = getRandomSlot(conn);
stmt.setString(1, names[i]);
stmt.setString(2, uniqueIds[i]);
stmt.setString(3, exams[i]);
stmt.setString(4, slots[i]);
stmt.setString(4, slot);
stmt.executeUpdate();
}
System.out.println("Student data inserted successfully!");
}
}

private static String getRandomSlot(Connection conn) throws SQLException {
String selectQuery = "SELECT slot FROM classes ORDER BY RAND() LIMIT 1";
try (Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(selectQuery)) {
if (rs.next()) {
return rs.getString("slot");
}
}
throw new SQLException("Failed to retrieve a random slot from the classes table.");
}

private static void displaySeatingArrangement(Connection conn) throws SQLException {
String selectQuery = "SELECT name, unique_id, exam, slot_number FROM students ORDER BY slot_number, name";
String selectQuery = "SELECT s.name, s.unique_id, s.exam, s.slot_number FROM students s ORDER BY s.slot_number, s.name";
try (Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(selectQuery)) {
System.out.println("Seating Arrangement:");
Expand Down
152 changes: 152 additions & 0 deletions term_cum_assign.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class term_cum_assign {
// 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 {
term_cum_assign window = new term_cum_assign();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
});
}

public term_cum_assign() {
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 <= 30; 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();
}
}
}