求助:关于java.sql.SQLException: Operation not allowed after ResultSet closed的问题

ailianhua 2006-06-29 03:56:00
我的源代码如下
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.sql.*,java.util.*,com.info.bean.*,com.info.inc.*"%>
<%@ include file="islogin.jsp"%>
<%@ include file="isadmin.jsp"%>
<%
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
PreparedStatement ps2 = null;
ResultSet rs2 = null;
conn=dbconn.getconnection();
comm commhtml = new comm();
String htmlhead ="";
htmlhead = commhtml.HtmlHead("友情提示");
out.println(htmlhead);


%>
<meta http-equiv=refresh content="600;URL=infolist.jsp">
<table align=center width='80%' border=0>
<tr><td>
<div align=left>
<B>回复信息提示</B>:(点击信息标题可以直接进入)
<br>
<%
String StrSql;
StrSql="select * from info_info where guidang=0 and recontent!='' and recontent is not null and isback=0";
StrSql= StrSql + " order by updatetime desc,dateyear desc,yearid desc limit 0,20";
//ps = conn.prepareStatement(StrSql);
ps= conn.prepareStatement(StrSql);
rs = ps.executeQuery();

while (rs.next())
{
%>
<FONT COLOR="#CC0033"><B><%=rs.getString("AdminUnit")%></B></FONT> 对 <a href="recontent.jsp?id=<%=rs.getString("Info_Id")%>"><FONT COLOR="#3333FF"><%=rs.getString("Info_Title")%></FONT></a> 信息做了回复 (<%=rs.getString("Info_Time").substring(0,10)%>)。<br>
<%

}




String StrSql2;
StrSql2="select * from info_info where isback=1 order by updatetime desc,dateyear desc,yearid desc limit 0,20 ";
//ps2 = conn.prepareStatement(StrSql2);
ps2 =conn.prepareStatement(StrSql2);
rs2 = ps2.executeQuery();
int r=0;

%>
<br><br>
<B>退回信息提示</B>:(点击信息标题可以直接进入)
<br>
<%

while (rs2.next())
{
r=1;
%>
<a href="recontent.jsp?id=<%=rs2.getString(1)%>"><FONT COLOR="#3333FF"><%=rs2.getString(9)%></FONT></a> 信息被退回 (<%=rs2.getString(11).substring(0,10)%>)。<br>
<%
}

if(r==1)
{
%>

<EMBED src="/nudge.wav" autostart="true" loop="false" width="0" height="0" hidden=true >
<%
}

%>
</td></tr></table>
</div>
<%
rs2.close();
rs2=null;
ps2.close();
ps2=null;
rs.close();
rs=null;
ps.close();
ps=null;
dbconn.closeconnection();
conn=null;
out.println(commhtml.HtmlTail());
commhtml=null;

目前的问题是,我是读取了两遍数据库,也是使用两个PreparedStatement 和两个ResultSet 来做的,但是它有时候就好使,没有问题,有时候刷新的话 就会出现java.sql.SQLException: Operation not allowed after ResultSet closed的错误,改了好多遍,都不知道错误在哪里,哪位大哥知道啊。愁死了!

十万分感谢啊!
...全文
886 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
ailianhua 2006-06-29
  • 打赏
  • 举报
回复
这样啊? 那还是通用办法,。 建议是通过ResultSet读取记录封装以后,然后放入ArrayList里面,再关闭ResultSet这样对于数据库的性能和数据安全都是最好的。

我是新学的jsp,你说的方法该怎么做啊

linshaojie 2006-06-29
  • 打赏
  • 举报
回复
这样啊? 那还是通用办法,。 建议是通过ResultSet读取记录封装以后,然后放入ArrayList里面,再关闭ResultSet这样对于数据库的性能和数据安全都是最好的。
ailianhua 2006-06-29
  • 打赏
  • 举报
回复
一个stmt最好对应一个rs, 如果用一个时间内用一个stmt打开两个rs同时操作,就会出现这种错误.

所以解决此类问题:1.就多创建几个stmt,一个stmt对应一个rs;2.若用一个stmt对应多个rs的话,那只能得到一个rs后就操作,处理完第一个rs后就close掉 再处理第二个rs试试。

就是把
rs.close();
rs=null;
ps.close();
ps=null; 放到rs处理完,准备掉rs2那 试试?

我已经这么做过了,一样的错误,最后改成现在这样的,还是出错啊

linshaojie 2006-06-29
  • 打赏
  • 举报
回复
就是把
rs.close();
rs=null;
ps.close();
ps=null; 放到rs处理完,准备掉rs2那 试试?
linshaojie 2006-06-29
  • 打赏
  • 举报
回复
一个stmt最好对应一个rs, 如果用一个时间内用一个stmt打开两个rs同时操作,就会出现这种错误.

所以解决此类问题:1.就多创建几个stmt,一个stmt对应一个rs;2.若用一个stmt对应多个rs的话,那只能得到一个rs后就操作,处理完第一个rs后就close掉 再处理第二个rs试试。
MBlue 2006-06-29
  • 打赏
  • 举报
回复
rs2=null;之类的不需要
MBlue 2006-06-29
  • 打赏
  • 举报
回复
rs2.close();
rs2=null;
ps2.close();
ps2=null;
rs.close();
rs=null;
ps.close();
ps=null;
dbconn.closeconnection();
conn=null;
把这些放在finally里做,加上try catch
kevinliuu 2006-06-29
  • 打赏
  • 举报
回复
dbconn.closeconnection();

没有传入get到的conn, 就能关闭conn, 你的方法如何实现的?

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧