为什么我一用就出现ResultSet已经关闭的错误?

supertoto 2002-09-10 11:40:22
在我自己的一个包里面我定义了数据库的连接和ResultSet的取值,但是一运行就报错说我的ResultSet已经关闭,请问为什么?
下面是我的jsp中使用到的几个语句:
<%
tree mytree =new tree(response);
mytree.test();
mytree.close();
%>

在我的java文件中是这样的:

package tree;

import java.sql.*;
import java.io.*;
import javax.servlet.http.HttpServletResponse;

public class tree
{
String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr = "jdbc:odbc:topics";
Connection conntemp = null;
ResultSet rs;
PrintWriter out = null;
ResultSet rsTest;

public tree(HttpServletResponse resp)
{
try
{
out=resp.getWriter();
}
catch (Exception e)
{
System.out.println("test():" + e.getMessage());
}
}


ResultSet executeQuery(String sql)
{
try
{

Class.forName(sDBDriver);
conntemp = DriverManager.getConnection(sConnStr);
Statement stmt = conntemp.createStatement();
//rs = stmt.getResultSet();
rs =stmt.executeQuery(sql);
stmt.close();
}
catch (Exception e)
{
System.out.println("aq.executeQuery: " + e.getMessage());
}
return rs;
}

public void close()
{
try {
conntemp.close();
rs.close();
//rsSubtopics.close();
//rsMainTopic.close();
//rsTest.close();
}
catch (Exception ex) {
System.out.println("ax0.exception: " + ex.getMessage());
}
}


public void test()
{
rsTest = executeQuery("Select * from topic");
try {
System.out.println (rsTest.getRow());
}
catch (Exception ex) {
System.out.println("Rs error: " + ex.getMessage());
}

}

}

...全文
243 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhu_liping 2002-09-10
  • 打赏
  • 举报
回复
stmt是不能关的,
利而不害 2002-09-10
  • 打赏
  • 举报
回复
rsTest = executeQuery("Select * from topic");
在executeQuery()函数返回ResultSet 之前你就已经把stmt.close();了
wjmmml 2002-09-10
  • 打赏
  • 举报
回复
stmt.close();
在关闭上面的前必须关闭rs。
supertoto 2002-09-10
  • 打赏
  • 举报
回复
楼上的兄弟,我已经加了 return rs;


to hazeline:对,我取消了stmt的关闭以后,我可以都出来一部分的数据
因为我有一部分的数据是用递归的方法调用一个函数的,递归的时候都不出来数据阿
saoyu 2002-09-10
  • 打赏
  • 举报
回复
呵呵,楼上两位没有说出真正错在何处
其实是在调用executeQuery时,函数没有返回rs记录集结果
所以rsTest没有得到函数返回值,如果你将rsTest改成rs(因为rs为类变量),可以得到结果,或者在executeQuery方法中最后添加return rs;就OK了

81,090

社区成员

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

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