JDBC always tests the last row of MySQL table?

后端 未结 2 740

I have a Manager class that saves data in the SQL table and also get result from SQL table and test these data.when I run my program,one frame will be shown that gets ID and pas

2条回答
  •  鱼传尺愫
    2021-01-23 18:31

    The simple answer is that your while loop should terminate when you find a match, so it should read:

        while (rst.next() && bool == false) {
            if (rst.getString(1).equalsIgnoreCase(userName) && rst.getString(2).equalsIgnoreCase(password)) {
                bool = true;
            }
        }
    

    Note that it would be more efficient to select only rows that match your user id & password, something along the lines of the following: (note that error handling is left as an exercise for the reader)

    PreparedStatement stmt; stmt = conn.prepareStatement("SELECT yahooId , password FROM clienttable WHERE yahooId = ?"); stmt.setString(1, "userName");
    ResultSet rst = null; rst = stmt.executeQuery();

    if (rs != null && rs.getString("password").equals(password)) { bool = true; }

提交回复
热议问题