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.
And the output reads as so:/**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());
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