Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to iterate ResultSet multiple times

 
Abrahim Daver
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

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





Thanks in advance.

Abra.
 
Scott Selikoff
author
Saloon Keeper
Posts: 4014
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rs.first()

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.
 
Abrahim Daver
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Scott,

Instead of caching the data OR hitting database again, Isn't it possible to iterate a resultset multiple times?

Thanks,
Abra
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look into the cursor options. You can specify a bi-directional cursor that will probably let you do this. This may have grave issues with memory use and connection life span.

Also look into the RowSet interface. That copies data from the ResultSet into non-connected Java object so you can release the connection and play with the data all day long. Memory concerns remain.
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Scott Selikoff
author
Saloon Keeper
Posts: 4014
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic