java中怎么嵌套读取数据库?
public List getListPetDiaryById(int id){
private Connection con=null;
private PreparedStatement ps=null;
private ResultSet rs=null;
List list=new ArrayList();
PetInfoDao petInfoDao=new PetInfoDao();
try {
con=DBHelper.getConnection();
String sql="select * from petdiary where diary_pet_id=?";
ps=con.prepareStatement(sql);
ps.setInt(1, id);
rs=ps.executeQuery();
while(rs.next()){
entity.PetDiary petDiary=new PetDiary();
petDiary.setDiary_author(rs.getString("diary_author"));
petDiary.setDiary_author_email(rs.getString("diary_author_email"));
petDiary.setDiary_context(rs.getString("diary_context"));
petDiary.setDiary_date(rs.getDate("diary_date"));
petDiary.setDiary_id(rs.getInt("diary_id"));
petDiary.setDiary_is_public(rs.getBoolean("diary_is_public"));
petDiary.setDiary_last_modify(rs.getDate("diary_last_modify"));
petDiary.setDiary_title(rs.getString("diary_title"));
petDiary.setDiary_weather(rs.getString("diary_weather"));
int diary_pet_id=rs.getInt("diary_pet_id");
DBHelper.closeAll(rs, ps, con);
petDiary.setPetInfo(petInfoDao.selectPetById(diary_pet_id));//这个方法是根据ID查的也是用的ResultSet
list.add(petDiary);
}
} catch (
SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DBHelper.closeAll(rs, ps, con);
return list;
}
我这个只能查出一条记录来,错误提示是ResultSet 已关闭,该如何写呢?
------解决方案--------------------第一个DBHelper.closeAll(rs, ps, con); 去掉试试。
------解决方案--------------------int diary_pet_id=rs.getInt("diary_pet_id");
//DBHelper.closeAll(rs, ps, con);