Can someone tell me, how can I iterate resultset more than one time. As I want to use same resultset multiple times, to avoid database trip for same query. I observed that I cannot iterate result set more than once, it sets it as null and throws null pointer exception. I am using following code
Although this type of behavior isn't always supported so I'd strongly recommend you either: 1) Cache all the information coming out of the result set to a local object so you don't need to read it again 2) Perform better queries since it sounds like you might be throwing away unused data each time the query is performed.
Originally posted by Abrahim Daver: Instead of caching the data OR hitting database again, Isn't it possible to iterate a resultset multiple times?
If you read through a ResultSet and then don't close it, you are essentially caching it from that point onward. So why not cache it in a more usable form, and why not close it to remove a resource that you are holding on to?
The answer to your original question is "yes", you can iterate over it multiple times in certain circumstances. What we're trying to point out is performance could be awful in such a situation and there's no valid reason why you would ever prefer to do it this way rather than cache it to a more usable object.