Move cursor to first row after while loop ResultSet
Asked Answered
H

1

2

If I have to loop two times after each other using while(rs.next()) for different reasons through the same ResultSet, what can I do so that the ResultSet doesn't close automatically and to prevent the 'ResultSet is closed Exception'? I tried using resultset.first() to move the cursor back to the first row after the first loop but this error fired even on this statement!

Haye answered 3/7, 2013 at 14:29 Comment(0)
P
2

This depends on your driver used.

Some driver are not able to reset a ResultSet. In this case you will get an Exception when calling first().

You can check in the Statement with a call to getResultSetType() if your driver supports scrolling in a ResultSet.

If possible try to do your two things in one iteration as you never know if you can iterate a second time over a ResultSet.

Promycelium answered 3/7, 2013 at 14:34 Comment(1)
I did the two things in one iteration to be safe. At least I could separate them! I am really finding problems with looping over result sets! Thanks for your help :)Haye

© 2022 - 2024 — McMap. All rights reserved.