Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Reading from ResultSet to Object and from object Object Array

  1. #1
    Junior Member anmaston's Avatar
    Join Date
    Apr 2011
    Location
    Plymouth, UK
    Posts
    10
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Angry 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.

            /**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:
    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
    Last edited by anmaston; April 6th, 2011 at 03:08 PM. Reason: example output


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,260
    Thanks
    176
    Thanked 818 Times in 761 Posts
    Blog Entries
    5

    Default 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.

  3. #3
    Junior Member anmaston's Avatar
    Join Date
    Apr 2011
    Location
    Plymouth, UK
    Posts
    10
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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 View Post
    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,

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,260
    Thanks
    176
    Thanked 818 Times in 761 Posts
    Blog Entries
    5

    Default Re: Reading from ResultSet to Object and from object Object Array

    To use the example of reading a ResultSet in partial pseudo-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).

  5. The Following User Says Thank You to copeg For This Useful Post:

    anmaston (April 7th, 2011)

  6. #5
    Junior Member anmaston's Avatar
    Join Date
    Apr 2011
    Location
    Plymouth, UK
    Posts
    10
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Reading from ResultSet to Object and from object Object Array

    ohhhh I seee what you mean, I should have got that from
    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

Similar Threads

  1. [SOLVED] How to declare an object of multi-dimension array?
    By FongChengWeng in forum Collections and Generics
    Replies: 7
    Last Post: January 14th, 2011, 12:17 AM
  2. Object array with constructor
    By thedolphin13 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 8th, 2010, 11:02 AM
  3. 2D Object makes my object smaller, Why?
    By MassiveResponse in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 15th, 2010, 02:33 PM
  4. Array object and constructors
    By TarunN in forum Collections and Generics
    Replies: 14
    Last Post: May 6th, 2010, 04:04 PM
  5. How do you set an object in array to null value?
    By Arius in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 25th, 2010, 02:50 AM

Tags for this Thread