• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do I put a ResultSet into an Array?

 
rastas biggs
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to get rows of data from the database and place in an Arraylist.

I'm not sure if an ArrayList is the right choice, I need to get my ArrayList and call the Column names to place the out put in a sorted order to print to a file for another system to process so it is imperative that data is in a specific order.

Can some one point me in the right direction.

RB
 
jefff willis
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Storing the output into an array, a list, or a vector is all simple enough, but you should let the DB do the sorting for you.

When you query for the data, use the ORDER BY phrase in your statement
 
rastas biggs
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jeff, do you have some example code of placing the result set into an array or vector

RB
 
jefff willis
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a code snippit:



This is just rudimentry. modify it to fit your needs.
 
rastas biggs
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cool thanks for that

RB
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do people insist on using Vector still?
It is well out of date - 7 years in fact.

Why oh why oh why?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you get multiple columns from your query you'll probably want to make a class that represents one row and put instances of that class into the collection:

If you really need to sort MyThings after the query (I agree with making the database do the sort if at all possible!) look into implementing Comparable in MyThing and using an ordered collection like TreeSet. Holler if that's not enough to go on.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic