0

I am running in to an odd issue. I have a database with 6 different "bids". I am trying to extract the bids with the following code:

public ArrayList<ProjectBid> getProjectBids(String projectID)
    {
        ArrayList<ProjectBid> result = new ArrayList<ProjectBid>();

        ProjectBid bid = new ProjectBid();

        try{

        st = con.createStatement();
        String query = "SELECT * FROM bids WHERE projectID=\""+projectID+"\"";
        System.out.println(query);
        ResultSet rs = st.executeQuery(query);


        while (rs.next())
          {
            bid.setProjectID(projectID);
            bid.setBidID(rs.getString("bidID"));
            bid.setBidderEmail(rs.getString("bidder_email"));
            bid.setBidAmount(rs.getInt("bid_amount"));
            bid.setBidMessage(rs.getString("bid_message"));
            bid.setTimestamp(rs.getString("timestamp"));

            result.add(bid);
          }
          st.close();

        }
        catch(Exception ex){
            System.out.println("Exception: "+ex);
        }

        return result;          
    }

For some reason my ArrayList result is giving me 6 identical "bid" objects instead of adding each of the "bids" from my database individually. The code runs through the ResultSet as expected and the correct values for each run is printed correctly if I add "System.out.println(bid.getBidID);" inside my while-loop. Can anyone tell me what is wrong with my code?

Thank you

3 Answers 3

3

You should move ProjectBid bid = new ProjectBid(); inside the while loop. With the current code you are updating the same bid variable which is defined outside the while loop hence it gets the last record's value after the loop completes.

Sign up to request clarification or add additional context in comments.

Comments

3

Add

ProjectBid bid = new ProjectBid();

in your while loop. As of now you are just overwriting the same ProjectBid object. So each reference in you ArrayList is pointing to the same object value of which you keep changing in your loop. So at the end you will have all elements in ArrayList with data same as last row retrieved from the database.

    while (rs.next())
      {
        ProjectBid bid = new ProjectBid();
        bid.setProjectID(projectID);
        bid.setBidID(rs.getString("bidID"));
        bid.setBidderEmail(rs.getString("bidder_email"));
        bid.setBidAmount(rs.getInt("bid_amount"));
        bid.setBidMessage(rs.getString("bid_message"));
        bid.setTimestamp(rs.getString("timestamp"));

        result.add(bid);
      }

8 Comments

Then I will create a new "bid" object for each row in my database. How will that effect my memory usage if I have 1000 bids? Shouldn't overriding the same object work just as fine?
@user3586514: No. First, you're going to be overwriting existing data with the current approach (this answer is correct); second, the lifecycle of each new ProjectBid object lasts only until the end of the loop; each one will be eligible for garbage collection.
No it will not. All add() method does it add a reference to the object you pass. So as I said earlier at the end of loop all your array list references will point to same object. You need separate objects to save your distinct database row entries.
@user3586514 You could define a variable outside the loop but you have to reassign it with a new operator inside the loop.
@user3586514 No it shouldn't and you already noticed that it doesn't work. And yes it effect your memory... a little bit. Don't care about that.
|
1

Since the reference to the bid is always the same thereby, it is overwriting the bid with the current value that in turn is changing all the bids in the result list giving you the last bid added.

Instantiate the bid object inside your loop and this should work fine.

while (rs.next())
  {
    ProjectBid bid = new ProjectBid();
    ...
    ...
    ...
    result.add(bid);
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.