为什么它只检查sql的最后一行?

Joh*_*nna -3 java sql

我在我的数据库类中使用此方法来检查密码和yahooId,如果它们是正确的,它允许用户转到下一帧.我在我的sql中添加了很多yahooId和密码,但这种方法只检查最后一行并允许最后一个人去下一帧.你能帮帮我吗?谢谢.

  public static boolean Test(String userName, String password) {
    boolean bool = false;
    Statement stmt = null;
    try {
        stmt = conn.createStatement();

        ResultSet rst = null;

        rst = stmt.executeQuery("SELECT yahooId , password FROM clienttable");


        while (rst.next()) {
            if (rst.getString(1).equals(userName) && rst.getString(2).equals(password)) {
                bool = true;
                break;
            } else {
                bool = false;
            }
        }
    } catch (SQLException ex) {
        Logger.getLogger(Manager.class.getName()).log(Level.SEVERE, null, ex);
    }
    System.out.println(bool);
    return bool;



}
Run Code Online (Sandbox Code Playgroud)

out*_*tis 7

当您只对其中一行感兴趣时,请勿选择所有行.使用WHERE子句,这是它的存在理由:

SELECT yahooId , password FROM clienttable WHERE yahooId=? AND password=?
Run Code Online (Sandbox Code Playgroud)

如果结果集为空,则身份验证失败.如果只有一个结果,则身份验证成功.如果有多个结果,那么您的数据集就会被清除(UNIQUE索引yahooID是防止这种情况的正确方法).

顺便说一句,问题来自于使用准备好的陈述,如果你以前没有见过它们的话.