• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to add resultset into ArrayList

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic