为什么我的"而(rs.next())"执行我使用的所有if,ifelse,else?

时间:2015-03-23 05:43:00

标签: java sql while-loop

这是我的代码:

String queryCheck = "SELECT username, password FROM LOGIN WHERE username='" + username + "' AND password='" + password + "'";
                     stmt.executeQuery(queryCheck);
                     ResultSet rs = stmt.getResultSet();
                     while(rs.next()) {
                          dbUserName = rs.getString("username");
                          dbPassword = rs.getString("password");
                         if (dbUserName.equals(username) && dbPassword.equals(password)) {
                              MemberOption Rbs = new MemberOption();
                              Rbs.setVisible (true);
                         }

                         else if (!dbUserName.equals(username) && !dbPassword.equals(password)){
                              int notif = JOptionPane.showConfirmDialog(this, "Your are not Registered! USERNAME does not match Go Back? ", "Closing Program",JOptionPane.YES_OPTION, JOptionPane.QUESTION_MESSAGE);
                               if(notif == 0) {
                               return;

                             }
                        else {
                        System.exit(0);
                    }
                }
               }

此声明未执行:

else if (!dbUserName.equals(username) && !dbPassword.equals(password)){
                              int notif = JOptionPane.showConfirmDialog(this, "Your are not Registered! USERNAME does not match Go Back? ", "Closing Program",JOptionPane.YES_OPTION, JOptionPane.QUESTION_MESSAGE);
                               if(notif == 0) {
                               return;

                             }
                        else {
                        System.exit(0);
                    }
                }

请帮忙

4 个答案:

答案 0 :(得分:1)

因为您的查询只返回具有usernamepassword的行,所以只有

if (dbUserName.equals(username) && dbPassword.equals(password))如果执行

答案 1 :(得分:0)

执行此代码

else if (!dbUserName.equals(username) && !dbPassword.equals(password))
{
                          int notif = JOptionPane.showConfirmDialog(this,     "Your are not Registered! USERNAME does not match Go Back? ", "Closing Program",JOptionPane.YES_OPTION, JOptionPane.QUESTION_MESSAGE);
                           if(notif == 0) {
                           return;

                         }
                    else {
                    System.exit(0);
                }
            }

您必须输入错误的用户名和密码才会执行​​!并检查用户名和&您提供的密码在最后或开头不包含额外的空格。

答案 2 :(得分:0)

您必须更改身份验证的逻辑。我建议使用以下逻辑进行密码验证。

String queryCheck = "SELECT password FROM LOGIN WHERE username='" + username + "'";
stmt.executeQuery(queryCheck);
ResultSet rs = stmt.getResultSet();
if(rs != null) {
    while(rs.next()) {
        dbPassword = rs.getString("password");
        if (dbPassword.equals(password)) {
            MemberOption Rbs = new MemberOption();
            Rbs.setVisible (true);
        }
        else {
            int notif = JOptionPane.showConfirmDialog(this, "USERNAME and PASSWORD does not match Go Back? ", "Closing Program",JOptionPane.YES_OPTION, JOptionPane.QUESTION_MESSAGE);
            if(notif == 0) {
                return;
            }
        }
    }
}else {
    int notif = JOptionPane.showConfirmDialog(this, "Your are not Registered! Go Back? ", "Closing Program",JOptionPane.YES_OPTION, JOptionPane.QUESTION_MESSAGE);
    if(notif == 0) {
        return;
    }
}

根据问题中的代码,如果查询没有返回任何结果,则不会输入rs.next()循环。

答案 3 :(得分:0)

预计查询只会产生与传递给它的条件有关的用户名和密码。所以显然if循环中的if条件只有在resultset不为null时才会被执行。更改用于测试elseif条件的表中没有的用户名或密码。