java.sql.SQLException: Cursor is closed.求解!

Leehoja 2012-12-26 04:53:28
本人初学者。求解决报错:java.sql.SQLException: Cursor is closed.
conn=JDBCUtil.getConn();

sql="{call pro_page(?,?,?,?,?,?,?,?,?)}";
CallableStatement cstmt=conn.prepareCall(sql);

cstmt.setString(1, pageBean.getOrderbyColumn());
cstmt.setString(2, pageBean.getOrderbyType());
cstmt.setString(3, pageBean.getReturnColumn());
cstmt.setString(4, pageBean.getTableName());
cstmt.setInt(5, pageBean.getCurrentPage());
cstmt.setInt(6,pageBean.getPageSize());
cstmt.setString(7, pageBean.getWhereSQL());
//输出参数,说明这是一个返回值
cstmt.registerOutParameter(8, oracle.jdbc.OracleTypes.NUMBER);
cstmt.registerOutParameter(9, oracle.jdbc.OracleTypes.CURSOR);
//执行
cstmt.execute();
//总记录数
int countrecord=cstmt.getInt(8);
pageBean.setTotalRecord(countrecord);

rs=(ResultSet)cstmt.getObject(9);


调试到rs=(ResultSet)cstmt.getObject(9); 时出现问题。

代码用的是老师的存储过程:
create or replace procedure pro_page(
v_in_orderbycolumn in out varchar2, --排序字段
v_in_orderbytype in out varchar2, --升序ASC或降序DESC
v_in_returncolumn in out varchar2, --返回字段
v_in_tablename in out varchar2, --表名
v_in_currentpage in out number, --当前页
v_in_pagesize in out number, --每页显示记录数
v_in_sqlwhere in out varchar2, --查询条件不带where,带and
v_out_totalrecord out number, --返回总记录数
v_out_cursor out pac_attrtype.cursortype --返回当前页数据
)
as
--声明变量
v_exc_notable exception;
v_startrow number;
v_endrow number;
v_sql varchar2(500);
begin
--1 处理输入参数的默认操作
if v_in_returncolumn is null then
v_in_returncolumn :='*';
end if;
--默认升序
if v_in_orderbytype is null then
v_in_orderbytype :='ASC';
end if;
if v_in_tablename is null then
raise v_exc_notable;
end if;
if v_in_currentpage is null then
v_in_currentpage :=1;
end if;
if v_in_pagesize is null then
v_in_pagesize :=5;
end if;
if v_in_sqlwhere is null then
v_in_sqlwhere :='';
end if;
--2 计算开始行和结束行
v_startrow :=(v_in_currentpage-1)*v_in_pagesize+1;
v_endrow :=v_in_currentpage*v_in_pagesize;
--3 拼接查询分页的SQL语句
if v_in_orderbycolumn is not null then
DBMS_OUTPUT.PUT_LINE(v_in_orderbycolumn);
v_sql :='select * from (select rownum rn,g.* from(select '||v_in_returncolumn||' from '||v_in_tablename||' where 1=1 '||v_in_sqlwhere||' order by '||v_in_orderbycolumn||' '||v_in_orderbytype|| ') g where rownum<='||v_endrow||') where rn>='||v_startrow;
else
v_sql :='select * from (select rownum rn,g.* from(select '||v_in_returncolumn||' from '||v_in_tablename||' where 1=1 '||v_in_sqlwhere||') g where rownum<='||v_endrow||') where rn>='||v_startrow;
DBMS_OUTPUT.PUT_LINE(v_sql);
end if;

--4 执行查询-动态的SQL,把结果给赋值v_out_cursor
open v_out_cursor for v_sql;
--5 查询总记录数赋值给v_out_totalrecord
v_sql :='select count(*) from '||v_in_tablename||' where 1=1 '||v_in_sqlwhere;
execute immediate v_sql into v_out_totalrecord;
exception
--异常处理
when v_exc_notable then
Dbms_Output.put_line('表和视图不存在!');
when others then
Dbms_Output.put_line('过程有误!');
end pro_page;



用老师的项目调用此存储过程没问题,大神来解救!
...全文
1619 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
大西瓜hubery 2014-04-16
  • 打赏
  • 举报
回复
我今天也遇到了,原因是8楼所说,查询的记录数为空,游标自动关闭。
tony4geek 2014-03-04
  • 打赏
  • 举报
回复
12 年的帖子。
longtao2009 2014-03-04
  • 打赏
  • 举报
回复
异常处理了吧 返回的cursor为空 所以java读取不到游标,导致java.sql.SQLException: Cursor is closed
isbn888 2012-12-26
  • 打赏
  • 举报
回复
java.sql.SQLException: Cursor is closed.错误提示到游标了,估计就是存储过程那里导致的
Leehoja 2012-12-26
  • 打赏
  • 举报
回复
搞不定啊,郁闷
handsome_huxiulei 2012-12-26
  • 打赏
  • 举报
回复
还没有结束使用游标的时候 把游标关闭了 (自己关闭或者程序异常然后自动关闭的), 查看下
Leehoja 2012-12-26
  • 打赏
  • 举报
回复
但在老师的项目里运行得好好的
笑莫问 2012-12-26
  • 打赏
  • 举报
回复
存储过程有问题吧,可以把存储过来在数据库中单独调试下。
Leehoja 2012-12-26
  • 打赏
  • 举报
回复
改了还是这个报错。。
菖蒲老先生 2012-12-26
  • 打赏
  • 举报
回复
cstmt.executeUpdate();

81,114

社区成员

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

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