Matt
Matt

Reputation: 1521

quit while loop whilst iterating through SQL table

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?

Upvotes: 1

Views: 190

Answers (2)

pickypg
pickypg

Reputation: 22332

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.

Upvotes: 3

havexz
havexz

Reputation: 9590

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

Upvotes: 4

Related Questions