Skip to content

Instantly share code, notes, and snippets.

@rapisenpai
Created May 24, 2022 15:16
Show Gist options
  • Save rapisenpai/87974a15f90bd7c5af3c08b38c447342 to your computer and use it in GitHub Desktop.
Save rapisenpai/87974a15f90bd7c5af3c08b38c447342 to your computer and use it in GitHub Desktop.
CONNECT LOGIN BUTTON TO DATABASE(MYSQL)
1. CREATE A METHOD
Connection con;
PreparedStatement pst;
ResultSet rs;
public void Connect() {
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3307/hospitalmanagementsystem", "root", "");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
Logger.getLogger(login.class.getName()).log(Level.SEVERE, null, e);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Logger.getLogger(login.class.getName()).log(Level.SEVERE, null, e);
}
}
2. ADD THIS TO THE BUTTON
String username = txtUsername.getText();
String password = new String(txtPassword.getPassword());
try {
pst = con.prepareStatement("SELECT * FROM login");
rs = pst.executeQuery();
while(rs.next()) {
String username1 = rs.getString("username");
String password1 = rs.getString("password");
if (username.equals(username1) && (password.equals(password1))) {
dashboard.main(null);
logInFrame.setVisible(false);
}
}
} catch (SQLException e1) {
Logger.getLogger(login.class.getName()).log(Level.SEVERE, null, e1);
}