我正在處理我的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