How to Create a Login Form in Java using MySQL Database? | Login and Registration in Java [With Source Code]
Code:
[clicking event of login button]
import java.sql.*
Connection con;
PreparedStatement pstmt;
ResultSet rs;
try {
try {
// TODO add your handling code here:
con = DriverManager.getConnection("jdbc:mysql://localhost/javalogin","root","admin");
} catch (SQLException ex) {
Logger.getLogger(LoginPage.class.getName()).log(Level.SEVERE, null, ex);
}
String query= "SELECT * FROM javalogin WHERE username=? AND password=?";
pstmt=con.prepareStatement(query);
pstmt.setString(1, txtUser.getText());
pstmt.setString(2, String.valueOf(jPasswordField2.getPassword()));
rs=pstmt.executeQuery();
if(rs.next()){
JOptionPane.showMessageDialog(null, "Login Success");
}else{
JOptionPane.showMessageDialog(null, "Login Error");
}
} catch (SQLException ex) {
Logger.getLogger(LoginPage.class.getName()).log(Level.SEVERE, null, ex);
}
show hide password:[in the clicking event of checkbox]
if(chkPass.isSelected()){
jPasswordField2.setEchoChar((char)0);
}else{
jPasswordField2.setEchoChar('\u25CF');
}
Register page opening code
Registration rs= new Registration(); rs.setVisible(true);
More: