0% found this document useful (0 votes)
71 views2 pages

JDBCARRAYLIST

The developer is trying to store the results of a ResultSet into an ArrayList but is only getting the last row repeated in the ArrayList size times. This is likely because the developer is reusing the same CustomerBean object in each loop iteration instead of creating a new one, so each element in the list refers to the same object. The suggested fix is to create a new CustomerBean on each loop to avoid overriding the object's fields.

Uploaded by

api-3818400
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views2 pages

JDBCARRAYLIST

The developer is trying to store the results of a ResultSet into an ArrayList but is only getting the last row repeated in the ArrayList size times. This is likely because the developer is reusing the same CustomerBean object in each loop iteration instead of creating a new one, so each element in the list refers to the same object. The suggested fix is to create a new CustomerBean on each loop to avoid overriding the object's fields.

Uploaded by

api-3818400
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

Hi guys,

I need a help. The situation is next: I’m trying to store some results of ResultSet into ArrayList. Unfortunately, when I’m
retrieving the data from ArrayList, the result, which I’m getting, is only the last row of a table. The result is repeating as
many times as ArrayList size is. Could somebody explain me why it happens?

There is a snippet of the code to get a data from the ResultSet and add it into ArrayList:

// Execute a query
String sql = "select * from user";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
ArrayList list = new ArrayList();
// Extract data from the result set
while(rs.next()) {

CustomerBean cb = new CustomerBean();


if(rs.getString("first_name")!=null)cb.setFirstName(rs.getString("first_name"));
if(rs.getString("last_name")!=null)cb.setLastName(rs.getString("last_name"));
if(rs.getDate("dob")!=null)cb.setDob(rs.getDate("dob"));

list.add(cb);

Another snippet of the code to retrieve the data from ArrayList:

for (int i = 0; i < cb.getUserData().size(); i++){

System.out.println("First -"+((CustomerBean)cb.getUserData().get(i)).getFirstName()); // hire an error

System.out.println("Last - "+((CustomerBean) cb.getUserData().get(i)).getLastName());// hire an error

System.out.println("Dob - "+((CustomerBean) cb.getUserData().get(i)).getDob());// hire an error

One thing you should change is the multiple calls on the same column to retrieve the columns value from your results set.
This can definately be problematic depending on the JDBC driver you're using.

I might suggest something like this:

// Execute a query

String sql = "select * from user";

Statement stmt = conn.createStatement();

ResultSet rs = stmt.executeQuery(sql);

ArrayList list = new ArrayList();

// Extract data from the result set

while(rs.next()) {

CustomerBean cb = new CustomerBean();

String firstName = rs.getString( "first_name" );

String lastName = rs.getString( "first_name" );


Date dateOfBirth = rs.getDate( "dob" );

if ( firstName != null )

cb.setFirstName( firstName );

if ( lastName != null )

cb.setLastName( lastName );

if ( dateOfBirth != null )

cb.setDob( dateOfBirth );

list.add( sb );

for (int i = 0; i < list.size(); i++){

System.out.println("First -"+((CustomerBean)list.get(i)).getFirstName());
// hire an error

System.out.println("Last -
"+((CustomerBean)list.get(i)).getLastName());// hire an error

System.out.println("Dob - "+((CustomerBean)list.get(i)).getDob());// hire


an error

Although this might not immediately solve your problem, it might help narrow down the area where the problem might
exist.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy