Resultset's next () is not a "method to determine if there is a ResultSet next".

It turns out that he had misunderstood about rs.next ().

I will post it, thinking that if someone made the same mistake.

About the interpretation of rs.next ()

I thought rs.next () was "a method that only determines if there is a ResultSet next",

In addition, there seems to be a function to "go to the next line from the current position and place the cursor". (For more information, see the Oracle page (https://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html).)

mistake.java


ResultSet rs = stmt.executeQuery(sql);

//If there is no result, null is returned
if(rs.next()){                       //← Here, the cursor is on the first line of the result
    return null;
}

//2.Generate a list
while (rs.next()) {                  //← Here, the cursor is on the second line of the result
    list.add(rs.getString("name"));  //← Here, the name of the second line is taken as a result
}

I was coding as above. Wrong. The list generated by java is one line less than hitting SQL with a command! I was worried for an hour ... That should be it too.

Since the cursor has hit the first line of the result before turning with while, When I tried to turn it with while, the cursor hit the second line of the result, The first line of the result was not added to the list.

Correct case

For reference, if you do the following, it seems that processing when the result is 0 is also applied.

Correct answer.java


ResultSet rs = stmt.executeQuery(sql);

//Add data to the list
while (rs.next()) {
    list.add(rs.getString("Field name"));
}

//If the list is from, return null
if(list.isEmpty()){
    return null;
}

Postscript

Some source code has been modified. Thank you for pointing out saka1029 and Kilisame!

Recommended Posts

Resultset's next () is not a "method to determine if there is a ResultSet next".
Resultset's next () was misunderstood as a method to determine if there is a next
Program to determine if it is a leap year
Use Modifier # isStatic to determine if the [Reflection] method is static.
If it is Ruby, it is efficient to make it a method and stock the processing.
If there is a state transition, let's create a State class
What to do if audio is not available on discordrb
Finally, create a method for whether there is any character
How to create a method
What to do when javax.el.ELException: Not a Valid Method Expression: appears when the JSF screen is displayed
[Ubuntu 20.04] What to do if the external monitor is not recognized
[Rails] What to do if data is not registered in DB
What to do if zip dies if there is a pom when making an executable jar with gradle
What to do if Operation not permitted is displayed when you execute a command in the terminal
Java Calendar is not a singleton.
To not be a static uncle
What to do if the background image is not applied after deployment
What to do if the prefix c is not bound in JSP
[Java] Conditional branching is an if statement, but there is also a conditional operator.
Get the type of an array element to determine if it is an array
If the HTTP method is not specified, the browser side requests with GET