Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to add resultset into ArrayList

 
tarun saha
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...

while(rs.next()){
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
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Move this line

into your while loop ... because you want to create an instance for every entry in the set.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic