Reading from ResultSet to Object and from object Object Array
Hi,
Firstly, I would like to apologise : I try to stay clear of your forums with my newbie skills and all, however it would be appreciated if somebody would be able to shed some light on this.
I was wondering, in Java, how would you suggest I go about placing a resultset from a database query within an object array? I know that this should be easy, and I had thought that this was the correct approach however when I call the array elements (product objects) the attribute of each object is exactly the same (noticably, all elements/object take the attributes of the last object that I tried to write into the array)
Any ideas? It would be greatly appreciated.
Code :
/**CREATING PRODUCT OBJECT ARRAY (ATTRIBUTE OF CUSTOMER OBJECT **/
i = -1
Product product = new Product;
Product products = new Product[20];
ResultSet productRS;
While(productRs.next()) {
i = i + 1; //increment i by 1
System.out.println(productRS.getString(1)); //This code for error checking
System.out.println(productRS.getString(2)); //runs as expected, displaying 4 complete records
System.out.println(productRS.getString(3));
System.out.println(productRS.getString(4));
System.out.println(productRS.getString(5));
product.setProductID(Integer.parseInt(productRS.getString(1)));//Copies the resultset items
product.setType(productRS.getString(2)); //accross to the product object
product.setModel(productRS.getString(3));
product.setName(productRS.getString(4));
product.setManufacturer(productRS.getString(5));
products[i] = product; //copy the new product to products array
System.out.println(products[i].getProductID()); //the value which is retrieved through this
//getter method which lives in "Product" class
//is the same as that of productRs.getString(1)
} // end of while loop
/**the value retrieved through products[#].getProductID() once outside the while loop is
always retrieved as 4, what on earth is going on?**/
System.out.println("element 0 :" +products[0].getProductID());
System.out.println("element 1 :" + products[1].getProductID());
System.out.println("element 2 :" + products[2].getProductID());
System.out.println("element 3 :" + products[3].getProductID());
And the output reads as so:
Code :
1 <---- Whilst inside loop System.out.println(productRS.getString(1));
EC <---- Whilst inside loop System.out.println(productRS.getString(2));
INDE312 <---- Whilst inside loop System.out.println(productRS.getString(3));
Indesit <---- Whilst inside loop System.out.println(productRS.getString(4));
Indesit Quickflame <---- Whilst inside loop System.out.println(productRS.getString(5));
1 <---- Whilst inside loop System.out.println(products[i].getProductID());
2 <---- Whilst inside loop System.out.println(productRS.getString(1));
GS <---- Whilst inside loop System.out.println(productRS.getString(2));
ELEC344 <---- Whilst inside loop System.out.println(productRS.getString(3));
Electrolux <---- Whilst inside loop System.out.println(productRS.getString(4));
Electrolux GasCooker <---- Whilst inside loop System.out.println(productRS.getString(5));
2 <---- Whilst inside loop System.out.println(products[i].getProductID());
3 <---- Whilst inside loop System.out.println(productRS.getString(1));
DW <---- Whilst inside loop System.out.println(productRS.getString(2));
HOOV223 <---- Whilst inside loop System.out.println(productRS.getString(3));
Hoover <---- Whilst inside loop System.out.println(productRS.getString(4));
Soapy Clean <---- Whilst inside loop System.out.println(productRS.getString(5));
3 <---- Whilst inside loop System.out.println(products[i].getProductID());
4 <---- Whilst inside loop System.out.println(productRS.getString(1));
WM <---- Whilst inside loop System.out.println(productRS.getString(2));
SAMS32 <---- Whilst inside loop System.out.println(productRS.getString(3));
Samsung <---- Whilst inside loop System.out.println(productRS.getString(4));
Samsung 10000rpm <---- Whilst inside loop System.out.println(productRS.getString(5));
4 <---- Whilst inside loop System.out.println(products[i].getProductID());
element 0 : 4 <---- After loop is finished "populating" the object array I call products[0].getProductID(); to test
element 1 : 4 <---- After loop is finished "populating" the object array I call products[1].getProductID(); to test
element 2 : 4 <---- After loop is finished "populating" the object array I call products[2].getProductID(); to test
element 3 : 4 <---- After loop is finished "populating" the object array I call products[4].getProductID(); to test
So, It would seem ,as i mentioned, that for some reason the last productID dealt with is becoming the productID for every single object inside the Product[], but I cannot understand why this should change once outside the loop o.O
my product
Re: Reading from ResultSet to Object and from object Object Array
While looping through the ResultSet, don't reuse the same object unless you've got reason to do so. Creating a new one for each row you read from the ResultSet will solve this issue - currently, each value in the array points to the same object, when you create new ones the values in the array will point to those unique objects.
Re: Reading from ResultSet to Object and from object Object Array
I think understand what you're saying, i'm pointing to the same object each iteration, therefore my array is only taking the object
productRS at its final position
I'm just unsure as to what you mean with the resolution :
Quote:
Originally Posted by
copeg
Creating a new one for each row you read from the ResultSet will solve this issue.
I presume that you don't mean the destination, Product object "product", because potentially, I wouldn't know how many sets of rows inside ResultSet;
and ResultSet is the whole collection of results, so I wouldn't split that into rows, surely?... I'm sorry, I'm sure that I'm being silly, or that its too late at night to think straight: but can you give me an example please?
Regards,
Re: Reading from ResultSet to Object and from object Object Array
To use the example of reading a ResultSet in partial pseudo-code:
Code :
Product[] myArray = new Product[somesize];
int count = 0;
while ( resultSet.next() ){
Product p = new Product();//create a new Object here
///set the values of p
myArray[count] = p;//assign this to the appropriate array index
count++;
}
The above code creates a new object per row (in the simplest sense, the ResultSet returns rows - for each call to next, the next database row is available).
Re: Reading from ResultSet to Object and from object Object Array
ohhhh I seee what you mean, I should have got that from
Quote:
a new object for each row
So its all working now lovely, I'm still not absolutely sure that I understand
what was happening before? by my logic my original code should have ammended
the appropriate attribute of product on each iteration(or maybe not). Is there a
simple explanation as to why it acts in this way?
Regards