我想确保当用户输入用户名和密码时,通过检查输入是否与用户表中的某些行匹配来完成身份验证。到目前为止的代码如下:它只允许数据库中的第一个用户登录。请建议我如何正确设置它。谢谢
private class thehandler implements ActionListener {
@Override
public void actionPerformed(ActionEvent ae) {
String namevalue = usertext.getText();
String pwdvalue = pwdtext.getText();
//read values from user table in sql database
try {
Class.forName("com.mysql.jdbc.Driver");
String conUrl = "jdbc:mysql://localhost/hall?" +
"user=root&password=blend";
Connection con = DriverManager.getConnection(conUrl);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM user");
while(rs.next()) {
if(namevalue.equals(rs.getString("userName")) && pwdvalue.equals(rs.getString("password"))) {
JOptionPane.showMessageDialog(null, "You are logged in",
"Makhall login", JOptionPane.INFORMATION_MESSAGE);
//move on to homepage if user is valid
homePage home = new homePage();
home.setAlwaysOnTop(rootPaneCheckingEnabled);
}
else {
JOptionPane.showMessageDialog(null, "Incorrect username or password",
"Error", JOptionPane.ERROR_MESSAGE);
}
break;
}
}
catch (SQLException e) {
System.out.println("SQL Exception: "+ e.toString());
}
catch (ClassNotFoundException cE) {
System.out.println("Class Not Found Exception: "+ cE.toString());
}
}
}