• Post Reply Bookmark Topic Watch Topic
  • New Topic

Arraylist problem

 
Ramon Haayema
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the connector

public ArrayList getAllAdresses (UserBean u){
if (con == null ){ setupConnection(u); }
ArrayList adressList = new ArrayList();
try {
AdressBean ab = new AdressBean();
stmt = con.createStatement();
rs =stmt.executeQuery("Select * from BO_ISERIES.pfadress");
while (rs.next()){
ab.setFirstName(rs.getString(1));
ab.setLastName(rs.getString(2));
ab.setTitel(rs.getString(3));
ab.setAdress(rs.getString(4));
ab.setZipCode(rs.getString(5));
ab.setCity(rs.getString(6));
ab.setTelephone(rs.getString(7));
ab.setMobileNr(rs.getString(8));
ab.setEMail(rs.getString(9));
adressList.add(ab);

System.out.println("Real one : "+ ab.getFirstName());
}
Iterator it = adressList.iterator();
while (it.hasNext()){
AdressBean wb = (AdressBean)it.next();
System.out.println(wb.getFirstName());
}

then the output is

[17-5-08 19:22:36:438 CEST] 508e3d44 WebGroup I SRVE0180I: [BO-Iseries] [/BO-ISeries] [Servlet.LOG]: Servlets.GetAllAdresses: init
[17-5-08 19:22:36:812 CEST] 508e3d44 SystemOut O Real one : Ramon
[17-5-08 19:22:36:812 CEST] 508e3d44 SystemOut O Real one : Karla
[17-5-08 19:22:36:812 CEST] 508e3d44 SystemOut O Real one : dikkie
[17-5-08 19:22:36:812 CEST] 508e3d44 SystemOut O dikkie
[17-5-08 19:22:36:812 CEST] 508e3d44 SystemOut O dikkie
[17-5-08 19:22:36:812 CEST] 508e3d44 SystemOut O dikkie
[17-5-08 19:22:36:812 CEST] 508e3d44 SystemOut O System numember : 0Beans.AdressBean@3f1bfd4e
[17-5-08 19:22:36:812 CEST] 508e3d44 SystemOut O System numember : 1Beans.AdressBean@3f1bfd4e
[17-5-08 19:22:36:812 CEST] 508e3d44 SystemOut O System numember : 2Beans.AdressBean@3f1bfd4e

so the adresslist hold the same bean.....
what is that wat am i missing?

please help....
 
Rob Spoor
Sheriff
Posts: 20817
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are using one single AddressBean object, and keep updating that one.

If you move "AdressBean ab = new AdressBean();" to inside the loop, you will create a new object for each iteration.
 
Muhammad Saifuddin
Ranch Hand
Posts: 1325
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ramon.

Welcome to Javaranch,

hope Rob reply cleared you on above question. if it doesn't, feel free to ask here.

use UBB Code functionality instead, it makes your post more readable specially in case of including code snipped.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!