-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseApp.java
More file actions
103 lines (87 loc) · 3.62 KB
/
Copy pathDatabaseApp.java
File metadata and controls
103 lines (87 loc) · 3.62 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class DatabaseApp extends JFrame implements ActionListener {
private JTextField usernameField;
private JPasswordField passwordField;
private JButton loginButton;
private int loginAttempts = 0;
private final int MAX_ATTEMPTS = 3;
private final String DB_URL = "jdbc:mysql://localhost:3306/ncat";
private final String DB_USER = "root";
private final String DB_PASSWORD = "yourpassword";
public DatabaseApp() {
setTitle("Database Login");
setSize(300, 170);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
JLabel usernameLabel = new JLabel("Username:");
JLabel passwordLabel = new JLabel("Password:");
usernameField = new JTextField(15);
passwordField = new JPasswordField(15);
loginButton = new JButton("Login");
loginButton.addActionListener(this);
JPanel panel = new JPanel(new GridLayout(3, 2, 5, 5));
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
panel.add(usernameLabel);
panel.add(usernameField);
panel.add(passwordLabel);
panel.add(passwordField);
panel.add(new JLabel());
panel.add(loginButton);
add(panel);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String username = usernameField.getText();
String password = new String(passwordField.getPassword());
String role = authenticateUser(username, password);
if (role != null) {
JOptionPane.showMessageDialog(this, "Login successful as " + role + "!");
if (role.equalsIgnoreCase("manager")) {
new ManagerDashboard(DB_URL, DB_USER, DB_PASSWORD);
} else {
JOptionPane.showMessageDialog(this, "Welcome Student! (Student features coming soon.)");
}
dispose(); // close login window
} else {
loginAttempts++;
if (loginAttempts >= MAX_ATTEMPTS) {
JOptionPane.showMessageDialog(this,
"Too many failed attempts. Application will now exit.",
"Access Denied", JOptionPane.ERROR_MESSAGE);
System.exit(0);
} else {
JOptionPane.showMessageDialog(this,
"Invalid credentials! Attempts remaining: " + (MAX_ATTEMPTS - loginAttempts),
"Login Failed", JOptionPane.WARNING_MESSAGE);
usernameField.setText("");
passwordField.setText("");
}
}
}
private String authenticateUser(String username, String password) {
String role = null;
try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD)) {
String query = "SELECT role FROM users WHERE username = ? AND password = ?";
try (PreparedStatement stmt = conn.prepareStatement(query)) {
stmt.setString(1, username);
stmt.setString(2, password);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
role = rs.getString("role");
}
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(this,
"Database connection error: " + ex.getMessage(),
"Error", JOptionPane.ERROR_MESSAGE);
}
return role;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(DatabaseApp::new);
}
}