1

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...");
}    
4
  • 3
    Can't you do query saying WHERE username = ? Not worth to try to get all the records from DB and iterate to see if they match. Commented Jun 15, 2017 at 16:24
  • 4
    Unrelated note: You shouldn't be storing passwords in plain text. Commented Jun 15, 2017 at 16:32
  • Also, consider single responsibility by splitting the DB lookup and the handling of the results of the lookup into different classes. Commented Jun 15, 2017 at 16:34
  • I know I shouldn't be stroring passwords as plain text.,.,its just a sample,.,and i'm also just a newbie in java Commented Jun 15, 2017 at 17:18

2 Answers 2

1

try this:

    try
    {
        boolean accountMatches = false;
        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)){
            accountMatches = true; 
            if(type==true){
            AdMenu admenu=new AdMenu();
            admenu.show();
            this.setVisible(false);       
            } else{

            Menu menu=new Menu();
            menu.show();
            this.setVisible(false); 
            }

        }
        if(accountMatches) break;

        }
    if(!accountMatches) 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...");
    }    
Sign up to request clarification or add additional context in comments.

Comments

0

You could have a flag var that you flip when the correct username and pass is found. And then use a break statement.

Then after the loop, check the flag. Like so...

    boolean flag = false;
    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); 
            }  
              flag = true;
              break;     
        }
      }
      if(flag == true){
          JOptionPane.showMessageDialog(null,"Ïncorrect username or password"); 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.