java 将结果集值放入 Collection 对象然后添加到 ArrayList
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17206523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Put resultset values into Collection object then add to ArrayList
提问by being_uncertain
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
?
我正在处理我的resultset
以获取详细信息。我需要返回 an ArrayList
,那么如何将 的键值放入resultset
任何集合对象,然后将相同的值放入 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;
}
回答by Andreas Dolk
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.
对于结果集中的每个结果,创建一个new Bike()
并将该结果中的值复制到新的自行车字段。最后,将自行车添加到列表中。
Bike bike = new Bike()
bike.setName(rs.getString("name"));
//...
bikeList.add(bike);
回答by Seshadri Sastry
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?
你会实例化一个 Bike 对象并设置你从结果集中读取的属性,然后将自行车对象添加到你的数组列表中,这不是你要找的吗?
回答by Suresh Atta
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;
回答by Rob
while (rs.next()) {
Bike bike = new Bike();
bike.setName(rs.getString("name"));
bike.setModel(rs.getString("model"));
bikeList.add(bike);
}
回答by jtomaszk
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)
}
回答by Masudul
You need to instantiate Bike in while loop and add to List
.
您需要在 while 循环中实例化 Bike 并添加到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;