-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegistrationGUI.java
More file actions
135 lines (114 loc) · 4.92 KB
/
RegistrationGUI.java
File metadata and controls
135 lines (114 loc) · 4.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
// Import required packages for GUI and database
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
// Registration window for student sign-up
public class RegistrationGUI extends JFrame {
// Input fields for student information
private JTextField usernameField, nameField, ageField, guardianNameField, guardianContactField;
private JPasswordField passwordField;
private JCheckBox minorCheckBox;
// Constructor - sets up the registration window
public RegistrationGUI() {
// Window settings
setTitle("Student Registration");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(8, 2)); // 8 rows, 2 columns layout
// Username input
add(new JLabel("Username:"));
usernameField = new JTextField();
add(usernameField);
// Password input (hidden)
add(new JLabel("Password:"));
passwordField = new JPasswordField();
add(passwordField);
// Full name input
add(new JLabel("Name:"));
nameField = new JTextField();
add(nameField);
// Age input
add(new JLabel("Age:"));
ageField = new JTextField();
add(ageField);
// Minor checkbox
minorCheckBox = new JCheckBox("Minor");
add(minorCheckBox);
add(new JLabel("")); // Empty cell for alignment
// Guardian name (shown only if minor is checked)
add(new JLabel("Guardian Name:"));
guardianNameField = new JTextField();
add(guardianNameField);
guardianNameField.setVisible(false); // Hidden by default
// Guardian contact (shown only if minor is checked)
add(new JLabel("Guardian Contact:"));
guardianContactField = new JTextField();
add(guardianContactField);
guardianContactField.setVisible(false); // Hidden by default
// Register button
JButton registerButton = new JButton("Register");
add(registerButton);
// Action for minor checkbox - show/hide guardian fields
minorCheckBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
boolean isMinor = minorCheckBox.isSelected();
guardianNameField.setVisible(isMinor);
guardianContactField.setVisible(isMinor);
}
});
// Action for register button - save student data
registerButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
registerStudent();
}
});
// Make window visible
setVisible(true);
}
// Method to save student registration to database
private void registerStudent() {
// Get text from all input fields
String username = usernameField.getText();
String password = new String(passwordField.getPassword());
String name = nameField.getText();
int age = Integer.parseInt(ageField.getText());
boolean isMinor = minorCheckBox.isSelected();
String guardianName = guardianNameField.getText();
String guardianContact = guardianContactField.getText();
// Try to save data to database
try (Connection connection = DatabaseConnection.getConnection();
PreparedStatement statement = connection.prepareStatement(
"INSERT INTO students (username, password, name, age, is_minor, guardian_name, guardian_contact) VALUES (?, ?, ?, ?, ?, ?, ?)")) {
// Set values in SQL query
statement.setString(1, username);
statement.setString(2, password);
statement.setString(3, name);
statement.setInt(4, age);
statement.setBoolean(5, isMinor);
statement.setString(6, guardianName);
statement.setString(7, guardianContact);
// Execute the insert query
statement.executeUpdate();
// Show success message
JOptionPane.showMessageDialog(this, "Registration successful!");
// Open login window and close registration window
new LoginGUI();
dispose();
} catch (SQLException ex) {
// Show error if database operation fails
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "Registration failed: " + ex.getMessage());
}
}
// Main method to start the registration window
public static void main(String[] args) {
// Create and show registration window
new RegistrationGUI();
}
}