This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes JDBC and Relational Databases and the fly likes How to add resultset into ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "How to add resultset into ArrayList" Watch "How to add resultset into ArrayList" New topic

How to add resultset into ArrayList

tarun saha

Joined: Mar 23, 2010
Posts: 27
well my question is how can i add the contains of the resultset into an arraylist in such a way that when i retrieve the arraylist they are all in object form.

So let me make in clear with the actual implementation.

my class is like:
public class DataBeans{
String id;
String name;

public setId()... assume
public setName()....

Now i have created its object and submitted in the database using jdbc. Works perfect.
I am also able to read this data in my database and it retrieving successfully

But i want to add this set of objects(the rows of the table) into Arraylist.

here is my code:

public fetch(){
DataBeans db = new DataBeans();
List<DataBeans> list = new ArrayList<DataBeans>();

ResultSet rs = smt.exe.... working...

db.setId(rs.getString("id")); //printing distinctly for each record successfully
db.setName(rs.getString("name")); //printing distinctly for each record successfully

list.add(db); // this is updating the contents of a single index and every time i read the list, all objects' value becomes the value of the last entered object.

So my basic question is how may i add the objects in a list uniquely, mine is rather overwriting the existing contents

the my data set in database is:

id name
A_1 A1
A_2 A2

but when i retrieve it the content of the list:

id name
A_2 A2
A_2 A2

please help
thanks for any help in advance.
Peter Taucher
Ranch Hand

Joined: Nov 18, 2006
Posts: 174
Move this line

into your while loop ... because you want to create an instance for every entry in the set.

Censorship is the younger of two shameful sisters, the older one bears the name inquisition.
-- Johann Nepomuk Nestroy
I agree. Here's the link:
subject: How to add resultset into ArrayList
jQuery in Action, 3rd edition