This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JDBC and the fly likes Conversion From resultset to Map Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Conversion From resultset to Map" Watch "Conversion From resultset to Map" New topic
Author

Conversion From resultset to Map

Abiodun Adisa
Ranch Hand

Joined: Jan 17, 2002
Posts: 495
Hello all, Please whats the easiest way to convert from Resultset into Map
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

That depends on what data you want in the Map.
Abiodun Adisa
Ranch Hand

Joined: Jan 17, 2002
Posts: 495
Originally posted by Paul Clapham:
That depends on what data you want in the Map.


Why would it depend on the type of value i want in the map. I already have a resultset that contains the results of a query i have executed and i want that value contained in a map
stu derby
Ranch Hand

Joined: Dec 15, 2005
Posts: 333
For each row in the resultset:
1. convert the row to an Object; only you can decide how to do that
2. pick a key for that Object; only you know what key might be appropriate
3. insert the Object into the map using the key
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
You can easily imagine a result set as a grid, or maybe a spreadsheet. One way to get that feeling in memory is an array of arrays, or a more flexible collection of collections. You can make a totally generic solution like this:

Now when you consume this thing you can get all the column information from the first object in the master list, then get one row of data from each of the subsequent objects in the master list.

I've done this for some generic tools, but wouldn't do it for a system-wide architecture. I'd much prefer to make an object for each unique query / set of result columns.


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
 
 
subject: Conversion From resultset to Map
 
Similar Threads
Retrieve Result Set data using core library
displaying key of a hashmap,wrong order
Can i filter Result set
Spring 101: DB Table to return a Map of Code,Description.
How to copy a SQL result into a Map