日期:2014-05-17  浏览次数:20894 次

急求解决 jsp 问题
<%@ 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

------解决方案--------------------
探讨

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

------解决方案--------------------
同一个页面用一个连接
------解决方案--------------------
在同一个页面不能使用两个连接的 按顺序用第二个的话会自动把第一个连接关闭的...我也遇到过....最后改成一个连接了