1

I want to add items from a SQL table to an Java ArrayList. To get each item in the table, I am using the loop:

while(results.next())

However, I believe that this won't quit the loop if there are no more results found unlike the .hasNext available from some classes and will cause an error of some sort. How would I get the while loop to quit if there is nothing else in the SQL table without causing an error?

0

2 Answers 2

4

Looking at the docs, i think it should work fine. Snippet from the docs:

Returns:
    true if the new current row is valid; false if there are no more rows

I hope you are talking about ResultSet

Sign up to request clarification or add additional context in comments.

Comments

3

Assuming that you are using a ResultSet, this is straight from the JavaDocs:

The next method moves the cursor to the next row, and because it returns false when there are no more rows in the ResultSet object, it can be used in a while loop to iterate through the result set.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.