• 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

ways to store results from query?

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

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.

E.g.
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
Shailan
 
author & internet detective
Posts: 41878
909
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shailan,
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.
 
Shailan Mm
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jeanne, and sorry for the cross post in the beginners forum.
 
I have a knack for fixing things like this ... um ... sorry ... here is a consilitory tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic