• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do I get a Subset of a ResultSet

 
Steve Dyke
Ranch Hand
Posts: 1619
1
  • Mark post as helpful
  • send pies
  • 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:

 
Manuel Leiria
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • 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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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.
 
Bill Cruise
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • 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.
 
Scott Selikoff
author
Saloon Keeper
Posts: 4010
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • 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 ]
 
Ashok Mor
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic