File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How to put a list of data retrieve from database into arraylist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to put a list of data retrieve from database into arraylist" Watch "How to put a list of data retrieve from database into arraylist" New topic
Author

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

jay lai
Ranch Hand

Joined: Apr 04, 2002
Posts: 180
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

Joined: Jan 09, 2005
Posts: 74
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,


Share Knowledge to gain it.<br /> <br />SCJP 1.2, SCDJWS 1.4, SCWCD 1.4, SCBCD 1.3, ICAD
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3704
    
    5

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.


My Blog: Down Home Country Coding with Scott Selikoff
jay lai
Ranch Hand

Joined: Apr 04, 2002
Posts: 180
Thanks Both,
I am going to try out.
Best Regards
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to put a list of data retrieve from database into arraylist