0

I was processing my resultset to get the details. I need to return an ArrayList, so how can I put the key,values from the resultset to any of the collection objects and then put the same to an ArrayList?

Here is the code:

public List<Bike> addBikes() throws ClassNotFoundException, SQLException{
        List<Bike> bikeList = new ArrayList<Bike>();

        Class.forName("com.mysql.jdbc.Driver");
        Connection con  = null;
        Statement  stm = null;
        ResultSet rs = null;
        con=DriverManager.getConnection("jdbc:mysql://localhost:3306/spring_hibernate","root","root"); ;
        stm=con.createStatement(); 
        String qry="Select * from bike"; 
        rs=stm.executeQuery(qry);
        while(rs.next())
        {           
            /* I need to put   
             * rs.getString("name"),rs.getString("model")
             * etc into any of the suitable collection objects 
             * and then need to add those to the ArrayList - bikeList
             * 
             */

        }
        return bikeList;

    }
2
  • What's the problem? What did you try? Commented Jun 20, 2013 at 6:19
  • 2
    But you almost have it! I gues you'll have to instantiate a Bike for each row, put name and model in there (Constructor or setters?) and then call bikeList.add(bike) ... that's it. Commented Jun 20, 2013 at 6:21

6 Answers 6

4

For each result in the result set, create a new Bike() and copy the values from that result to the new bikes fields. At the end, add the bike to the list.

Bike bike = new Bike()
bike.setName(rs.getString("name"));
//...
bikeList.add(bike);
Sign up to request clarification or add additional context in comments.

1 Comment

Is there a way to do this in a class-agnostic way, i.e. without having to go over every property? Can it be cleanly converted to a List<Object>?
3

You have Banana in your hand..just eat it :)

Create a empty list and in iteration Create new Bike and add into the List.

 List<Bike> bikes  = new ArrayList<Bikes>();
  while(rs.next())
        {           
           Bike bike = new Bike();
           bike.setname( rs.getString("name"));
           //other properties
           bikes.add(bike);
        }

    return bikes;

Comments

2

you would instantiate a Bike object and set properties that you read from result set, then add the bike object to your arraylist, isnt it what you are looking for?

Comments

2
while (rs.next()) {
    Bike bike = new Bike();
    bike.setName(rs.getString("name"));
    bike.setModel(rs.getString("model"));
    bikeList.add(bike);
}

Comments

2

I don't know how Your Bike class look like, but you should do it like this way:

while(rs.next())
{
    String column1 = rs.getString("column1_name");
    .... and the others columns

    Bike bike = new Bike();

    bike.setColumn1(column1);
    .... and others...

    bikeList.add(bike)
}

Comments

0

You need to instantiate Bike in while loop and add to List.

 List<Bike> bikes  = new ArrayList<>();
 while(rs.next())
    {           
       Bike bike = new Bike();
       bike.setname( rs.getString("name"));
       //other properties
       bikes.add(bike);
    }

  return bikes;

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.