This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JDBC and the fly likes How to iterate ResultSet multiple times Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to iterate ResultSet multiple times" Watch "How to iterate ResultSet multiple times" New topic
Author

How to iterate ResultSet multiple times

Abrahim Daver
Ranch Hand

Joined: Aug 06, 2004
Posts: 69
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
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3703
    
    5

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.


My Blog: Down Home Country Coding with Scott Selikoff
Abrahim Daver
Ranch Hand

Joined: Aug 06, 2004
Posts: 69
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

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3703
    
    5

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to iterate ResultSet multiple times
 
Similar Threads
Using a Bean for Retrieving Results Sets - Best Approach
Resultset.next() taking more time
Check the result set
How can i continue executing other SQL Statments , even if one of them fails
nested ResultSets