• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Formatting outputs based on values in resultset

 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I have a condition in which my query will return db results like

1 a
1 b
1 c
2 a
2 b
3 a

etc

I need output like
1 a,b,c
2 a,b
3 c
created in java. And I am thinking about how to manipulate results from the resultset for getting the above result.

I was thinking about creating a hashmap with
key/value pair as 1 a,b,c
2 a,b
3 c
Is that a good idea ?
I need more ideas to achieve this output
Thanks
Atul
 
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Atul Mishra,
With my limited knowledge what I prefer is :

1) Get the values the db returns & store it in a file
dbresults.txt


2) Next use a forloop(i=1 to 3) & stringtokenizer to parse through the tokens.
Set the stringtokenizer delimiter as i, so for i=1 you have
a b c

3) Store the i value in a String array of array with length 3

4) Now get values from array & use string formatter like String.format.

This is my opinion , but which I haven't tried.
Regards.
 
Atul Mishra
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ramya,

Thanks for your suggestion. However when I said 1,2,3 thats just an example. DB can have many more values also,

And writing output and creating a txt file can cause more overhead also.

Thanks anyway.
Keeping my fingers crossed.
Someway if I can get an String,List or something based on values from resultset, thats what I am hoping for
Atul
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any replies?
 
Marshal
Posts: 75714
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are probably better off creating a Map for sorting those values. But you cannot have two values for the same key, so what you might do is make the "V" part of the Map a List; then use the 1, 2, 3 to get the Lists from the Map, and add the a b c to the Lists.
You might need to put the keys into a List, sort them, and then use them to get the values; that way they will be in order. Remember an ordinary Map loses insertion order, but there are linked Maps which retain insertion order.
 
Every snowflake is perfect and unique. And every snowflake contains a very tiny ad.
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic