• 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 do I get a Subset of a ResultSet

 
Ranch Hand
Posts: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am getting a result set from a remote data source. After the resultset is returned how can I do a select on the result to return a subset of records?

My code:

 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The best way, in my opinion, is store the values in one ArrayList


now, close the result set ( as soon you release the result set, better).
You have the result set inside the arrayList and you can iterate and get what ever you want
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The best thing you can do is modify your SQL statement so that it only returns the data that your application needs. That way, the database doesn't have to waste time on retrieving and sending back to your program the data that you are going to throw away.
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Jesper above. If you only want one subset of the ResultSet you should modify the SQL.

However, if you need more than one subset, Manuel's approach is a good one. You can usually subdivide the ArrayList in one iteration. Note that you probably don't want to try to do this when you're iterating over the ResultSet. This leads to delays in closing the ResultSet.
 
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
Jesper is right, you shouldn't query for data that you are going to throw away unless the performance difference is nominal (such as you were using 99% of the data).

A lot of ppl try to 'play' with result sets as is they are normal java objects, and you should keep in mind they are not. They are transport objects designed to ship information to/from the database and have their own very special properties. It would be like taking a moving van 'out for a spin on a racetrack'. If you have more specialized needs for the data, get it out of the resultset first into your own optimized data structure. As I mentioned to someone who posted on something similar the other day, its short work to turn a resultset into an array of Hash Maps.
[ July 18, 2007: Message edited by: Scott Selikoff ]
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, You can work while modifying an SQL query, It also take less time in terms of execution database operation. Further after getting ResultSet you can initiate an bean instance for each row you get from table and store it into array list.

And later you can process on araylist, no need to go for searching into database.





Ashok Mor
reply
    Bookmark Topic Watch Topic
  • New Topic