<%@ page language="java" contentType="text/html; charset=gbk"
pageEncoding="gbk"%>
<%@ page import="java.sql.*"%>
<%
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("cont") + "</td></tr>";
if (rs.getInt("isleaf") != 0) {
tree(conn, rs.getInt("id"), 1);
}
}
rs.close();
stmt.close();
conn.close();
%>
<%! String str = "";private void tree(Connection conn, int id, int level) {
Statement stmt = null;
ResultSet rs = null;
String preStr = "";for (int i = 0; i < level; i++) {
preStr += "----";
}
try {
stmt = conn.createStatement();
String sql = "select * from article where pid=" + id;
rs = stmt.executeQuery(sql);while (rs.next()) {
str += "<tr><td>" + rs.getInt("id") + "</td><td>" + preStr
+ rs.getString("cont") + "</td></tr>";
if (rs.getInt("isleaf") != 0) {
tree(conn, rs.getInt("id"), level + 1);
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
rs = null;
}
if (stmt != null) {
stmt.close();
stmt = null;
}
if (conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>Insert title here</title>
</head>
<body>
<table border="1">
<%=str%>
</table>
</body>
</html>错误提示是:
type Exception reportmessagedescription The server encountered an internal error () that prevented it from fulfilling this request.exceptionorg.apache.jasper.JasperException: Exception in JSP: /ShowArticleTree.jsp:118: Statement stmt = conn.createStatement();
9: ResultSet rs = stmt.executeQuery("select * from article where pid =0");
10:
11: while (rs.next()) {
12: str += "<tr><td>" + rs.getInt("id") + "</td><td>"
13: + rs.getString("cont") + "</td></tr>";
14: if (rs.getInt("isleaf") != 0) {Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:451)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:355)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)root causejavax.servlet.ServletException: Operation not allowed after ResultSet closed
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:841)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:774)
org.apache.jsp.ShowArticleTree_jsp._jspService(ShowArticleTree_jsp.java:127)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)root causejava.sql.SQLException: Operation not allowed after ResultSet closed
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1056)
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
com.mysql.jdbc.ResultSetImpl.checkClosed(ResultSetImpl.java:770)
com.mysql.jdbc.ResultSetImpl.next(ResultSetImpl.java:7001)
org.apache.jsp.ShowArticleTree_jsp._jspService(ShowArticleTree_jsp.java:95)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)note The full stack trace of the root cause is available in the Apache Tomcat/5.5.25 logs.

解决方案 »

  1.   


    if (rs.getInt("isleaf") != 0) { 
    tree(conn, rs.getInt("id"), 1); 
    } 你声明的函数tree(conn, rs.getInt("id"), 1) 在第一次循环的时候会把你传入的数据库链接conn关闭,同时你的rs也会被关闭。循环再调用
    rs.next()方法时候就会有异常发生。
      

  2.   


    if (rs.getInt("isleaf") != 0) { 
    tree(conn, rs.getInt("id"), 1); 
    } 你声明的函数tree(conn, rs.getInt("id"), 1) 在第一次循环的时候会把你传入的数据库链接conn关闭,同时你的rs也会被关闭。循环再调用
    rs.next()方法时候就会有异常发生。
      

  3.   

    java.sql.SQLException: Operation not allowed after ResultSet closed 写的很明显啊:你在关闭ResultSet后,又使用ResultSet.
      

  4.   

    你定义的那个tree的方法,内部的stmt和rs可能于之前的stmt和rs冲突了,建议改一个名字.
    另外rs.close()之后再rs=null;是没有任何实际意义的哈,没有必要.
    在java里面,如果一个查询完了,rs.close()就行了,之后rs.和stmt都可以复用.
    如果需要嵌套查询,则需要重新定义stmt1和rs1.另外最好在rs.next()前判断一下rs!=null.不知道这一点我的理解对不对,自己一直这么用.