Resultset's next () was misunderstood as a method to determine if there is a next

Event

With some code, throw a SELECT query, There was a place where the acquisition result was stored in the ** ResultSet type variable rset **.

I wanted to check the number of elements in rset for reading the code, so I added the following code in the middle and executed it.

example.java


int testIndex = 0;
while(rset.next()){
    System.out.println("I want to check the number of elements in rset");
    System.out.println(++testIndex);
}

Then (although it was completed normally until then) In the process after that, the following exception occurred.

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

The reading comprehension was supposed to be broken

When I thought it was strange, I found that next () was wrongly understood. There is also a function to move the cursor from the current position to the next line.

In the above code, I exited the loop with the cursor on the last element, so In the subsequent rset, there is no element to which the cursor is next, The above exception has occurred.

At the same time I want to be careful about next () of ResultSet I learned that the code I added for reading comprehension can be bad.

Articles that I used as a reference

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

Recommended Posts

Resultset's next () was misunderstood as a method to determine if there is a next
Resultset's next () is not a "method to determine if there is a ResultSet 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
if case let b as Int = a is faster than if let b = a as? Int
Finally, create a method for whether there is any character
How to create a method
WSL2 There was only a steady way to get Ubuntu20.04 back to WSL1
What to do if zip dies if there is a pom when making an executable jar with gradle