<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ page import="java.sql.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><%!
String str = "";
private void tree(Connection conn, int id) { 
Statement stmt = null;
ResultSet rsrs = null;
try{
stmt = conn.createStatement();
String sql = "select * from article where pid = " + id;
rsrs = stmt.executeQuery(sql);
while(rsrs.next()) {
str += "<tr><td>" + rsrs.getInt("id") + "</td><td>" +
  rsrs.getString("title") + "</td></tr>";
if(rsrs.getInt("isleaf") != 0) {
tree(conn, rsrs.getInt("id"));
}
}
} catch (SQLException e) { 
e.printStackTrace();
} finally { 
try {
if(rsrs != null) {
rsrs.close();
rsrs = null;
}
if(stmt != null) {
stmt.close();
stmt = null;
}
if(conn != null) {
conn.close();
conn = null;
}
} catch(SQLException e) {
e.printStackTrace();
}
}
}%><%
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost/bbs?user=root&password=root";
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from article where pid = 0");
while(rs.next()) {
str += "<tr><td>" + rs.getInt("id") + "</td><td>" +
   rs.getString("title") + "</td></tr>";
if(rs.getInt("isleaf") != 0) {
tree(conn,rs.getInt("id"));
}
}

rs.close();
stmt.close();
conn.close();
%><html>
  <head>
    <base href="<%=basePath%>">
    <title>My JSP 'ShowArticleTree.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
  </head>
 
  <body>
   <table border = 4>
   <%= str %>
   </table>
  </body>
 
</html>
错误信息提示:java.sql.SQLException: Operation not allowed after ResultSet closed

解决方案 »

  1.   

    /*
    if(rsrs != null) {
    rsrs.close();
    rsrs = null;
    } */
    //这个应该会把rsrs关掉的
    if(stmt != null) {
    stmt.close();
    stmt = null;
    }
    if(conn != null) {
    conn.close();
    conn = null;
    }
      

  2.   


    把这一段拿出来,放在<%
    Class.forName("com.mysql.jdbc.Driver");
    String url = "jdbc:mysql://localhost/bbs?user=root&password=root";
    Connection conn = DriverManager.getConnection(url);
    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery("select * from article where pid = 0");
    while(rs.next()) {
    str += "<tr><td>" + rs.getInt("id") + "</td><td>" +
    rs.getString("title") + "</td></tr>";
    if(rs.getInt("isleaf") != 0) {
    tree(conn,rs.getInt("id"));
    }
    }放置位置
    %>
      

  3.   

    if(rsrs != null) {
    rsrs.close();
    rsrs = null;
    }
    ResultSet 关闭了
      

  4.   

    哥,你tree方法里已经关闭并置null了 外面就不能用了
      

  5.   

    我刚解决了这个问题,我来说一下,
    conn是传进来的参数,
    在递归方法tree递归到最后一层就关掉了传进来的参数,
    当递归最后一层结束返回到倒数第二层是,用到了rsrs参数,所以,就报错了。