大家帮忙看看,其实程序简单,帮我看看,运行产生:ResutlSet is closed.请大家指教。万般感谢
//:pagescount.java
package fanye;import java.sql.*;public class pagescount {
public static Connection getConnection() throws SQLException {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
return DriverManager.getConnection("jdbc:odbc:bookDB", "sa", "sa");
}
}
//:pages.jsp
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.sql.*"%>
<%@ page import="fanye.*"%>
<jsp:userBean id="userBean" scope="page" class="fanye.pagescount"/>
<%
Connection conn=null;
Statement stat=null;
ResultSet rs=null;
try{
conn=pagescount.getConnection();
stat=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stat.executeQuery("SELECT * FROM book");
}
finally{
if(conn!=null)
conn.close();
}
while(rs.next()){
%>
<html>
<head>
<title>
pages
</title><font size=2>
<table width="600" border="1" align="center" cellpadding="1" cellspacing="1">
<tr>
<td>书籍编号</td>
<td><%=rs.getString("BookDB")%></td>
<td>出版日期</td>
<td> </td>
<td>总页数</td>
<td> </td>
<td>价格</td>
<td> </td>
</tr>
<tr>
<td>书籍名称</td>
<td colspan="5"> </td>
<td>作者</td>
<td> </td>
</tr>
<tr>
<td>备注</td>
<td colspan="7"> </td>
</tr>
</table>
</font>
</body>
</html>
<%}%>
//:pagescount.java
package fanye;import java.sql.*;public class pagescount {
public static Connection getConnection() throws SQLException {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
return DriverManager.getConnection("jdbc:odbc:bookDB", "sa", "sa");
}
}
//:pages.jsp
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.sql.*"%>
<%@ page import="fanye.*"%>
<jsp:userBean id="userBean" scope="page" class="fanye.pagescount"/>
<%
Connection conn=null;
Statement stat=null;
ResultSet rs=null;
try{
conn=pagescount.getConnection();
stat=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stat.executeQuery("SELECT * FROM book");
}
finally{
if(conn!=null)
conn.close();
}
while(rs.next()){
%>
<html>
<head>
<title>
pages
</title><font size=2>
<table width="600" border="1" align="center" cellpadding="1" cellspacing="1">
<tr>
<td>书籍编号</td>
<td><%=rs.getString("BookDB")%></td>
<td>出版日期</td>
<td> </td>
<td>总页数</td>
<td> </td>
<td>价格</td>
<td> </td>
</tr>
<tr>
<td>书籍名称</td>
<td colspan="5"> </td>
<td>作者</td>
<td> </td>
</tr>
<tr>
<td>备注</td>
<td colspan="7"> </td>
</tr>
</table>
</font>
</body>
</html>
<%}%>
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.sql.*"%>
<%@ page import="fanye.*"%>
<jsp:userBean id="userBean" scope="page" class="fanye.pagescount"/>
<%
Connection conn=null;
Statement stat=null;
ResultSet rs=null;
try{
conn=pagescount.getConnection();
stat=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stat.executeQuery("SELECT * FROM book");while(rs.next()){
%>
<html>
<head>
<title>
pages
</title><font size=2>
<table width="600" border="1" align="center" cellpadding="1" cellspacing="1">
<tr>
<td>书籍编号</td>
<td><%=rs.getString("BookDB")%></td>
<td>出版日期</td>
<td> </td>
<td>总页数</td>
<td> </td>
<td>价格</td>
<td> </td>
</tr>
<tr>
<td>书籍名称</td>
<td colspan="5"> </td>
<td>作者</td>
<td> </td>
</tr>
<tr>
<td>备注</td>
<td colspan="7"> </td>
</tr>
</table>
</font>
</body>
</html>
<%}
finally{
if(conn!=null)
conn.close();
}}%>
close conn,会把rs也关闭,因此,对于你的代码,请先取完数据之后在关闭conn
if(conn!=null)
conn.close();
}这样:
<%
if(conn!=null)
conn.close();
}
}%>
Connection conn=null;
Statement stat=null;
ResultSet rs=null;
try{
conn=pagescount.getConnection();
stat=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stat.executeQuery("SELECT * FROM book");
}
finally{
if(conn!=null)
conn.close();//错误在这边
}
while(rs.next()){//conn都close掉了rs怎么不会close掉
%>
if(conn!=null)
conn.close();
}
应该在执行完while(rs.next()){后执行