java.sql.SQLException: Illegal operation on empty result set. when authenticating

后端 未结 2 1160
盖世英雄少女心
盖世英雄少女心 2021-01-15 11:00

What I\'m trying to do is:

  1. Accept username(uname) and password(passw) as an input from the user.

  2. Using ResultSet, retrieve the

相关标签:
2条回答
  • 2021-01-15 11:31

    @Eran mentioned the error by which a wrong id would yield an empty result set on which fields were gotten.

    I still have small remarks:

    • Try-with-resources take care of closing even in case of an exception or returning.
    • For a local database you can send the password to SQL.
    • Best to store passwords encrypted, when ever your database should get stolen.

    Thus:

    boolean loggedIn = false;
    try (PreparedStatement stmt =
            conn.prepareStatement("SELECT 1 FROM admin WHERE id = ? AND pass = PASSWORD(?)")) {
        stmt.setString(1, uname);
        stmt.setString(2, passw);
        try (ResultSet rs = stmt.executeQuery()) {
             loggedIn = rs.next();
        } // Closes rs.
    } // Closes stmt.
    
    0 讨论(0)
  • 2021-01-15 11:38

    You are supposed to use the result of rs.next() :

                if (rs.next()) {
                    n=rs.getString("id");
                    m=rs.getString("pass");
                }
    

    If rs.next() returns false, this means the query returned no rows.

    0 讨论(0)
提交回复
热议问题