简体   繁体   中英

Is it possible for a ResultSet.next() to return empty even if the database is populated?

I asked a question about this JDBC Login application I'm developing on Eclipse not recognizing the credentials I enter in the form. You might want to read that first to understand my problem.

After playing around with my code (and doing several tests to check if the JDBC driver is loaded and built into the class path), I figured that the error has more to do with the ResultSet not returning the specific row I requested from my query. I just wanted to know if this is a common occurrence or did I code something wrong?

Here's the (edited) code of the Login class:

    String driver = "com.mysql.jdbc.Driver";
    String url = "jdbc:mysql://localhost:3306/game"+"?verifyServerCertificate=false"+"&useSSL=true";
    String user = "root"; 
    String pass = "password";
    String sql = "SELECT username, password FROM player WHERE username = ? AND password = ?";

    String username = "uname";
    String password = "pword";

    try {
        Class.forName(driver);
        Connection connection = DriverManager.getConnection(url, user, pass);
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1, username);
        statement.setString(2, password);

        ResultSet resultSet = statement.executeQuery();
        boolean loginSuccess = false;

        while (resultSet.next()) {
            loginSuccess = true;
            System.out.println("Congrats! You are logged in as " + resultSet.getString("username") + ".");
        }
    } catch (Exception e) {
        throw new RuntimeException("Login failed", e);
    } 

Any sort of help is greatly appreciated. Thank you!

Before the question was edited, one could see, that you are reading the credentials from a request. Did you check if the username and password contain the correct values (log them out on console)? Yesterday I updated my answer in your other question. There your input fields had no name, so the values for user and password may be empty when you try to get them out of the request.

explanation can be found here: Does form data still transfer if the input tag has no name?

Solved this. The query was correct, but the ResultSet returned empty because for some reason, the value from input name = "pword" could not be read (returning null when I tried to print its value to the console), therefore not finding a match in the database. Thought it had something to do with the input being type password (which is weird because it should work that way). What I did was I changed input type to text, and that worked, except the characters in my the password field on my form could be seen. Changed it back to input type = password, and that somehow fixed my problem. No idea why that happened, but the redirects are working now. Thanks, @ErikP, for your help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM