我正在处理我的resultset
以获取详细信息。我需要返回一个ArrayList
,所以我怎样才能把resultset
的密钥值从任何集合对象中提取出来,然后把它们放到ArrayList
?将结果集值放入Collection对象,然后添加到ArrayList
下面是代码:
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;
}
什么问题?你尝试了什么? – yatul
但你几乎拥有它!我想你必须为每一行实例化一个“Bike”,把名字和模型放在那里(构造函数或setters?),然后调用'bikeList.add(bike)'...就是这样。 – Fildor