File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes ways to store results from query? 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 "ways to store results from query?" Watch "ways to store results from query?" New topic

ways to store results from query?

Shailan Mm

Joined: Oct 06, 2004
Posts: 8

I was wondering what the common ways to store the results of a JDBC query were. I have recently migrated from Perl, and am used to storing the results in a hash. Is there a similar data structure that can easily handle the results of the query. I have tried HashMap, but cant figure out how to get a key to map to another HashMap internally without explicitly creating the internal HashMaps first.

The query is something like this:
select animal, type, name from animals;

and the result set is:

Animal Type Name
dog husky Bella
dog poodle Alfie
cat manc Gus

I want the data structure to look like this:

dog => {husky => Bella, poodle => Alfie}
cat => {manc => Gus}

that is:
1) 2 top level keys - dog and cat
2) the 'dog' key pointing to another HashMap which itself has 2 keys - husky and poodle, with the value for husky being Bella and the value for poodle, Alfie
3) the 'cat' key pointing to another HashMap containing one key-value pair, manc => Gus

Alternatively, is there some better way to store the results rather than trying to duplicate Perl hashes?

Thank you
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33111

You are correct that you have to create the internal HashMaps first. But if you write one or two utility methods, this shouldn't be a big deal.

I typically store my results in an ArrayList because my applications don't have any need for the multiple levels you are trying for.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Shailan Mm

Joined: Oct 06, 2004
Posts: 8
Thanks Jeanne, and sorry for the cross post in the beginners forum.
I agree. Here's the link:
subject: ways to store results from query?
It's not a secret anymore!