Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to put a list of data retrieve from database into arraylist

 
jay lai
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I am writing a class that invoke a storeprocedure return multiple reccords from database.
While (rs.next())
{
string test1=rs.getString("testValueFromDB");
string test2=rs.getString("test2ValueFromDB");
string test3=rs.getString("test3ValueFromDB");
}
What do I do to put this resultset into an array and loope through it?
Any hints or suggestion is greatly appreciated.
 
Nadeem Awad
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by jamie lee:

What do I do to put this resultset into an array and loope through it?
Any hints or suggestion is greatly appreciated.


I think the best way is to wrap the String values in one object for each row, and then add the objects to the ArrayList. Something like this:


Best regards,
 
Scott Selikoff
author
Saloon Keeper
Posts: 4014
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agreed with Nadeem's solution, except the creation of a "MyValueObject" really depends on how frequently you need this object. Its it of a very temporary nature and you know the number of columns returned by the query, I suggest a typed array such as:



This is just from my experiences in practice, only create a new class if it is appropriate to do so. It depends on a number of other factors though.
 
jay lai
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Both,
I am going to try out.
Best Regards
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic