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.ResultSet;
import java.sql.SQLException;
public class Login extends
JFrame implements
ActionListener {
private JTextField
usernameField;
private JPasswordField
passwordField;
private JButton loginButton;
public Login() {
setTitle("Login");
setSize(300, 200);
setDefaultCloseOperation(JFram
e.EXIT_ON_CLOSE);
setLayout(new
GridLayout(3, 2));
add(new
JLabel("Username:"));
usernameField = new
JTextField();
add(usernameField);
add(new
JLabel("Password:"));
passwordField = new
JPasswordField();
add(passwordField);
loginButton = new
JButton("Login");
add(loginButton);
loginButton.addActionListener(thi
s);
setVisible(true);
}
@Override
public void
actionPerformed(ActionEvent e) {
String username =
usernameField.getText();
String password = new
String(passwordField.getPasswo
rd());
try (Connection conn =
DBConnection.getConnection()) {
String sql = "SELECT *
FROM users WHERE username
= ? AND password = ?";
PreparedStatement
statement =
conn.prepareStatement(sql);
statement.setString(1,
username);
statement.setString(2,
password);
ResultSet resultSet =
statement.executeQuery();
if (resultSet.next()) {
JOptionPane.showMessageDialo
g(this,
"Login successful!");
new POSSystem();
dispose();
} else {
JOptionPane.showMessageDialo
g(this, "Invalid username or
password.");
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
public static void main(String[]
args) {
new Login();
}
}