• 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

ArrayList storing only last table record

 
Greenhorn
Posts: 16
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Everyone ,

I have a problem in this code.I am storing table record in "arrayList" and passing it to another servlet. But the problem is , arraylist is producing only one record multiple times. If table has 3 records, then this arraylist showing only last record 3 times. Please help to get full table record in the arrayList. Many Thanks

try {
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
String projectName = rs.getString("projectname");
String projectCode = rs.getString("projectcode");
projectVO.setProjectName(projectName);
projectVO.setProjectCode(projectCode);
arrayList.add(projectVO);

}
} catch (Exception e ) {
e.printStackTrace();
}
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It appears that you don't create a new instance of your DTO class you're using for storing the record for each row in the resultset. So in all probability your list contains N references to the same instance (a List collection allows that). The projectName and projectCode fields of the projectVO instance are overwritten with each pass of the loop, and when the loop finishes, it keeps the last value that was encountered in the loop.

You should create a new instance in the loop. Add a projectVO = new TheDTOClassName(); line into the loop before setting its values.

(Please UseCodeTags the next time. It makes your code much easier to read.)
 
Savannah David
Greenhorn
Posts: 16
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Martin. It really worked . I will definitely use "UseCodeTags" in my further posts. Thanks once again for the valuable suggestion.
 
Savannah David
Greenhorn
Posts: 16
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
-
 
reply
    Bookmark Topic Watch Topic
  • New Topic