public List<Category> showcategory() {
Connection conn = null;
Statement st = null;
ResultSet rs1 = null;
ResultSet rs2 = null;
ResultSet rs3 = null;
List<Category> list = new ArrayList<Category>();
try {
conn = DaoFactory.getConnection();
st = conn.createStatement();
String sql = "select * from tonlion_product_categoryone";
rs1 = st.executeQuery(sql);
while(rs1.next())
{
Category one = new Category();
one.setId(rs1.getInt(1));
one.setName(rs1.getString(2));
list.add(one);
sql= "select * from tonlion_product_categorytwo where tpc_id = "+ one.getId();
rs2 = st.executeQuery(sql);
while(rs2.next())
{
Category two = new Category();
two.setId(rs2.getInt(1));
two.setName(rs2.getString(2));
list.add(two);
sql="select * from tonlion_product_categorythree where tpc_two_id ="+ two.getId();
rs3 = st.executeQuery(sql);
while(rs3.next())
{
Category three = new Category();
three.setId(rs3.getInt(1));
three.setName(rs3.getString(2));
list.add(three);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}finally{
DaoFactory.closeAll(conn, st, null);
}
return list;
}
在主函数中遍历集合:
public static void main(String[] args) {

CategoryDao c = new CategoryImpl();
List<Category> list = c.showcategory();
for(int i = 0; i < list.size();i++)
{
Category t = list.get(i);
System.out.println(t.getName());
}

}遍历结果如下:【一楼】休闲绅士馆
男冬装热卖
冬装羽绒服(男)
秋装羊毛衫(男)
com.microsoft.sqlserver.jdbc.SQLServerException: 结果集已关闭。
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:171)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.checkClosed(SQLServerResultSet.java:345)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.next(SQLServerResultSet.java:915)
at com.tonlion.daoImpl.CategoryImpl.showcategory(CategoryImpl.java:55)
at com.tonlion.daoImpl.CategoryImpl.main(CategoryImpl.java:83)我想说的是为什么会报错:结果集已关闭,敲断点是在第三个循环完之后转到第二个循环rs2.next()的时候停止的,要怎么办啊!请大家帮忙看看怎么解决,先谢谢了哈

解决方案 »

  1.   

    你这是最基本的ResultSet,你试试可保持结果集看行不,
    Statement st=createStatment (int resultsetscrollable,int resultsetupdateable,int resultsetSetHoldability)
    ResultSet rs=st.excuteQuery(sqlStr)
      

  2.   

    一个Connection只能有一个打开的结果集,所以当你executeQuery的时候,上一个结果集已经关闭了。楼上说的应该是对的,你需要使用持久结果集,也就是说executeQuery就已经把结果集全部从数据库取回来,并且在JVM中保存好了。此外,持久结果集才能支持游标定位滚动等操作。