• Post Reply Bookmark Topic Watch Topic
  • New Topic

Formatting outputs based on values in resultset  RSS feed

 
Atul Mishra
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • 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
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Any replies?
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!