I am making a little login program in Java and I have a little problem: In checking if the username or password matches an entry in my database, I use the next()-iterator, and I want it to display "Incorrect username or password" if the data entered doesn't match the data on my database.
The problem is every time I test it and try to login with an incorrect username or password, the iterator checks the entries of my database one by one and displays "Incorrect username or password" every query. I just want it to display the message as soon as it has checked all the data in my database. Here's my code; I hope someone could help me...
try
{
String un=username.getText();
String pw=password.getText();
Connection conn = DriverManager.getConnection("jdbc:ucanaccess://C:\\Users\\JGUTX\\Documents\\UserDatabase.accdb");
Statement s=conn.createStatement();
ResultSet rs=s.executeQuery("SELECT UserName,Password,Administrator FROM UserDatabase");
while(rs.next()){
boolean type=rs.getBoolean("Administrator");
String dbUser=rs.getString("UserName");
String dbPass=rs.getString("Password");
if(un.equals(dbUser)&& pw.equals(dbPass)){
if(type==true){
AdMenu admenu=new AdMenu();
admenu.show();
this.setVisible(false);
} else{
Menu menu=new Menu();
menu.show();
this.setVisible(false);
}
}else{
JOptionPane.showMessageDialog(null,"Ïncorrect username or password");
}
}
}
catch (Exception e)
{
System.err.println("Got an exception!");
System.err.println(e.getMessage());
JOptionPane.showMessageDialog(null, "Error connecting to database...");
}