my dog learned polymorphism
The moose likes JDBC and Relational Databases 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
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Conversion From resultset to Map" Watch "Conversion From resultset to Map" New topic

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

Joined: Oct 14, 2005
Posts: 19973

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
I agree. Here's the link:
subject: Conversion From resultset to Map
It's not a secret anymore!