Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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

 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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,
 
author
Posts: 4323
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
jay lai
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Both,
I am going to try out.
Best Regards
 
A feeble attempt to tell you about our stuff that makes us money
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic