I need some help with an ArrayList problem, I will write the code and expalin it and at the end will describe the problem.
JSP =========================================================================
1)I get a java.sql.ResultSet from an
ejb to my jsp,
resultSet=myejb(vendor,start_date,end_date);
2)I set the individual columns in an array and pass the array to a bean method
that justs adds the array to an ArrayList
//check if any records exist in the ResultSet
legit=resultSet.next();
while(legit){
//set the ResultSet coulmns in the details Array
details[0]=resultSet.getString("ITEM_NO");details[1]=resultSet.getString("QUANTITY_NO");details[2]=resultSet.getString("WHSE_NO");details[3]="description goes here";
//pass the populated array to a method in a class file, this class file just
adds the Array to an Arraylist(scroll to end for references)(ref#1)
cms_utilities.BookingsDetailSetRow(details);
//check for more records in the ResultSet
legit=resultSet.next();
}
3) and then I just want to print out the Array list
//declare a new Array
ArrayList BookingsDetails= new ArrayList();
//call a method in the cms_utilities class (ref#2) that returns the populated ArrayList, in step 2
BookingsDetails=cms_utilities.getBookingsDetail();
int counter=0;
int ArrayListSize=BookingsDetails.size();
//loop though the Arraylist
while(counter<ArrayListSize){
//convert Arraylist's rows into an Array to print them
String[] details_array= (String[]) BookingsDetails.get(counter);
out.print(details_array[0]);
out.print(details_array[1]);
out.print(details_array[2]);
out.print(details_array[3]);
counter++;
}
========================================================================
cms_utilities class
//arraylist decalred as global in the class
ArrayList detail_array_list=new ArrayList();
(ref#1)
public void BookingsDetailSetRow(String[] detail_array)
{
//populate Arraylist
detail_array_list.add(detail_array);
}
(ref#2)
public ArrayList getBookingsDetail()
{
//return populated ArrayList
return detail_array_list;
}
===========================================================================
The problem is that even though I am adding the correct elements to the ArrayList, I confirmed this because I did a printout of every array Array I passed to the method and it is the correct information being set.
However when I get the populated ArrayList, I get only the last set of data in all the records, in other words I get the correct number of rows but they are all populated with the last Array added to the ArrayList.
I am really certain it is not a loop or something I am missing somewhere, I have spent a day already checking all the details.
Another thing I have noticed is that it all works well if I add one record at a time, in other words, if I take the loop out and add one record at a time the ArrayList will keep on incrementing the correct data and diplay it in the correct manner, so my hunch is that somehow when I add records in the loop it looses all the information except the last record.
Any help will be greatly appreciated.
[ January 20, 2004: Message edited by: Sergio Barreros ]