0

Everything is perfect when I'm logging in. But when I enter an account that doesn't exist, a message dialog loops by the number of account there is in my database. I believe this is because I used while(rss.next()). Here is my code:

try {
    if(e.getSource()==loginButton){
        int count = 1;
        conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=login_DB;integratedSecurity=true");
        st=conn.createStatement();
        rss=st.executeQuery("select * from tblLogin");
        while(rss.next()){
            String user = rss.getString(1);
            String pass = rss.getString(2);
            if(usernameTF.getText().equals(user)&&passwordTF.getText().equals(pass)){
                JOptionPane.showMessageDialog(null,"YEAAAA");
            }//if success

            else {
                JOptionPane.showMessageDialog(null,"Account doesn't exist! Please try again.");
            }
        }//while
    }//getsource loginButton
2
  • 1
    Why don't you use a WHERE clause to get just the candidate records (should be at most one, actually), instead of all of them? Commented May 25, 2015 at 13:00
  • I created a database which only has 1 table, and only has username and password. I know its pretty stupid hahaha but tbh, Im still practicing creating a connection between the two ^_^ Commented May 25, 2015 at 13:13

3 Answers 3

1

Do something like this..

int flag = 0;
if(usernameTF.getText().equals(user) && passwordTF.getText().equals(pass))
{
    flag=1; // if found then, value change to 1 otherwise it will be 0
}

//Now, put below condition after while loop
if (flag==1) {
    JOptionPane.showMessageDialog(null,"YEAAAA");
}
else {
    JOptionPane.showMessageDialog(null,"Account doesn't exist! Please try again.");                
}

OR

As @Thilo suggested, you can use WHERE clause in your SELECT query.

Sign up to request clarification or add additional context in comments.

3 Comments

Nice idea :D It actually worked! I'll update you if I a logic problem comes up XD
Better use a boolean flag for this. And there are things like continue, break, return, too for flow control.
@Thilo boolean could be better option. Yes, of course, we can use continue in else condition before dialog box code. But, in that case dialog box will never popup. :)
1

Instead of selecting all from TblLogin, put a WHERE Clause in the query so it only pulls from the database Where that username is true, then (assuming you've made it so only one username per account) there will be a result returned if the username is present, you can then compare hashes or passwords depending on what you've used. Example :

String queryCriteria = "SELECT Username, Password FROM TblLogin WHERE Username = ? ";

 statement= connection.prepareStatement(queryCriteria );
  statement.setString(1,usernameEntered);
  resultSet = stmt.executeQuery();

Comments

0

You need to use a local variable like 'found' how I set it below. When you find your match, you print your message and mark found = true. After you exit your loop you check the value of found and print your error message if needed.

if(e.getSource()==loginButton){
        int count = 1;
        conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=login_DB;integratedSecurity=true");
        st=conn.createStatement();
        rss=st.executeQuery("select * from tblLogin");
        boolean found = false;
        while(rss.next()){
        String user = rss.getString(1);
        String pass = rss.getString(2);
        if(usernameTF.getText().equals(user)&&passwordTF.getText().equals(pass)){
            JOptionPane.showMessageDialog(null,"YEAAAA");
            found = true;
        }//if success
        }//while
    if(found)
            JOptionPane.showMessageDialog(null,"Account doesn't exist! Please try again.");
}//getsource loginButton

2 Comments

I somehow get your point but can you explain what 'if(found)' do? I'm new to that xD I mean its my first time seeing an if statement with that expression :D
The if(found) tests if found == true It tests if we found the specific account that we are looking for

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.