• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to iterate ResultSet multiple times

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Poop goes in a willow feeder. Wipe with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic