我都已经正常关闭ResultSet了啊?可是数据库中共有10条信息,发布后只是显示5条,然后就会报operation not allowed after resultset closed的错误,说我操作前把结果集关闭了。我知道一个Statement和一个ResultSet对应出现,但是我都有改不了。具体问题代码如下:
<%!
//递归的方法,所在的楼层不是叶子节点,pid=id,指的是对id楼层的回复,找出这些集合取出数据保存按树形结构输出
String str = ""; //设置用于保存数据库中信息的变量
private void tree(Connection conn,int id,int level){
Statement stmt1 = null;
ResultSet rs1 = null;

//level变量是调整格式,输出时是树形的结构
String prelevel = "";
for(int i=0;i<level;i++){
prelevel +="----";
}

try{
stmt1 = conn.createStatement();
String sql = "select * from article where pid ="+id;
rs1 = stmt1.executeQuery(sql);
while(rs1.next()){
//保存回复了id楼的信息数据,需要把回复的信息按树形的结构输出
str += "<tr><td>"+rs1.getInt("id")+"</td><td>" +
prelevel+rs1.getString("title")+"</td></tr>";

System.out.println(rs1.getString("title"));

if(rs1.getInt("isleaf")!=0){
//如果不是叶子节点继续进行递归调用,指向帖子的下一级
tree(conn,rs1.getInt("id"),level+1);
}

}
}catch(SQLException e){
System.out.println("显示部分数据后,就会抛出异常!ReslutSet被关闭了!");
e.printStackTrace();
}finally{
try{
if(rs1 != null){
rs1.close();
rs1 = null;
}
if(stmt1 != null){
stmt1.close();
stmt1 = null;
}
if(conn != null){
conn.close();
conn = null;
}
}catch(SQLException e1){
//System.out.println("@@@@@@@@@@@@@@@");
e1.printStackTrace();
}
}
}%>
<%
//连接数据库
//创建Driver
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost/bbs?user=root&password=fxz";
//得到Connection
Connection conn = DriverManager.getConnection(url);
//创建Statement
Statement stmt2 = conn.createStatement();
//找出帖子的主题, 即pid = 0时就是发的主题帖子,看是否有回复(有无叶子节点),id为评论的楼层数,pid 为要回复的楼层数
String sql = "select * from article where pid = 0";
ResultSet rs2 = stmt2.executeQuery(sql);
try{
//取出有主题的帖子,判断是否是叶子节点,不是就再次进行递归,是就是尾贴
while(rs2.next()){
//如果仅是主题贴 没有回帖 也要将其显示出来
str += "<tr><td>"+rs2.getInt("id")+"</td><td>" +
rs2.getString("title")+"</td></tr>";
if(rs2.getInt("isleaf")!=0){
//调用递归方法
tree(conn,rs2.getInt("id"),1);
}
}
}catch(SQLException e){
System.out.println("***************");
e.printStackTrace();
}finally{
try{
if(rs2 != null){
rs2.close();
rs2 = null;
}
if(stmt2 != null){
stmt2.close();
stmt2 = null;
}
if(conn != null){
conn.close();
conn = null;
}
}catch(SQLException e1){
System.out.println("%%%%%%%%%%%%%%%%%%%%%");
e1.printStackTrace();
}
}
%>我调了很久 也不明白是啥意思~~请大家帮忙看看啊~

解决方案 »

  1.   

    我想我大概明白你的意思,这里你的递归用的不好。
    比如 
    f(conn,n) = f(conn, n-1) * n   // conn是数据库的连接,n是其他的参数不用关心
    f(conn,2) = f(conn, 1) * 2     // n递减到1,这是最后一次递归
    f(conn,1) 
    {
      ...
      conn.close();                // 这个执行后,f(conn,2)中的conn是同一个,也被关闭了啊

    你的递归不应该把conn做为参数的。
      

  2.   

    我试过了啦,把那个conn.close()去掉就好了!呵呵 谢谢啦 果然是高手啊~