1 Reply Latest reply on Feb 26, 2005 9:41 AM by starksm64

    Error with Arraylist object returning from SessionBean

    asty

      Hi,
      I am accessing a database from a stateless session bean through a DAO. I am accessing all the fields and storing the values in an object of a class I have made. The class has datamembers correspondng to the fields of the table and getter and setter methods.
      I am storing all the records as objects in an ArrayList object. This is the code of DAO Implementation class:

      try{
       con=jdbcFactory.getConnection();
       String query="select * from itemgeneralDetails";
       ps=con.createStatement();
       rs=ps.executeQuery(query);
       while(rs.next())
       {
      
       System.out.println(rs.getString("itemname"));
      
       itemDetails.setItemID(rs.getString("itemid"));
       itemDetails.setItemName(rs.getString("itemname"));
       itemDetails.setCategory(rs.getString("category"));
       itemDetails.setSubcategory1(rs.getString("subcategory1"));
       itemDetails.setSubcategory2(rs.getString("subcategory2"));
       itemDetails.setSellingprice(Double.parseDouble(rs.getString("sellingprice")));
       itemDetails.setSellingUnit(rs.getString("sellingunit"));
       itemDetails.setDiscount(Double.parseDouble(rs.getString("discount")));
       itemDetails.setWeight(rs.getString("weight"));
       itemDetails.setImageURL(rs.getString("imageURL"));
       System.out.println(itemDetails.getItemName());
       list.add(i,itemDetails);
       String testName=((ItemDetails)list.get(i)).getItemName();
       System.out.println("Name from list " + testName);
       i++;
       }
      return(list);
      


      This is giving correct result when I see the output in the log file. That is all the records are added to the list.
      But when I access this list object from my client I find that all the elements of the list have the values of last record. This is the client code:
      ArrayList result=new ArrayList();
      result=myBean.giveResults();
       System.out.println(result.isEmpty());
       int k=result.size();
      
       //System.out.println("Showing RecordNo. " + i);
       itemDetails=(ItemDetails)(result.get(1));
      
      
       for(i=0;i<k;i++)
       {
       System.out.println("Showing RecordNo. " + i);
       itemDetails=(ItemDetails)result.get(i);
      
       System.out.println("Name : "+ itemDetails.getItemName());
       System.out.println("ID : "+ itemDetails.getItemID());
       System.out.println("Category : "+ itemDetails.getCategory());
       }


      It prints the same thing again and again though value of i is incremented. Why is this eror coming?
      Please help.
      Asty.