• 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

adding multiple rows to a list

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have retrived multiple rows from database and now i want to add each row to a collection.. i dont know the number of rows i retrieved .. so how do i add each row to the list
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You add them one at a time. Each time you read a row from the database, you add it to the list. It isn't necessary to know in advance how many rows you are going to add.

Now if you had chosen an array, you would have had to know in advance. That's why choosing a list instead of an array, as you did, is a good idea.
 
Ranch Hand
Posts: 198
Oracle Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Create your own bean with the attributes present in the row, and set all the results to the bean, and finally add the bean to a list. This iteration is for every record in the result set.

see below link for examples. hope that helps.

Data access using JDBC
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Create your own bean as Prabhakhar said
2. Create a generic collection for your bean for example ArrayList<YourBean>
3. use the addAll(Collection list) method to pass the retrieved http://docs.oracle.com/javase/1.4.2/docs/api/java/util/ArrayList.html#addAll(java.util.Collection)


Also, if you mean you don't know the number of items you retrieved, then its simple. Get an iterator and use its hasNext method to determine if there is another object after the current pointer. If there is, then use the next() method in a loop to retrieve the object and keep adding in a collection. Also, if you dont want to use iterator, use the resultset.next method, and keep adding the object returned in a Collection till rs.next() returns false i.e. no more records left.

Take this code as a sample:



Hope this makes sense and helps.
 
When it is used for evil, then watch out! When it is used for good, then things are much nicer. Like this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic