首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

java.sql.SQLException: 结果集已耗尽,该怎么解决

2012-01-08 
java.sql.SQLException: 结果集已耗尽我用oracle数据库,建了两个表结构如下:单位表:id_danweinumber,name_

java.sql.SQLException: 结果集已耗尽
我用oracle数据库,建了两个表结构如下:
单位表:id_danwei   number   ,name_danwei   varchar2   ,其中id_danwei是主键。
人员表:id_renyuan   number,   id_danwei   number   ,   name   varchar2   ,其中id_renyuan是主键,id_danwei是外键。

我用jsp对表管理,删除单位表中id_danwei=3的记录时,先查看人员表里是否有id_danwei=3的记录,如果有则不允许删除,如果没有则可以删除。
代码如下:

<%@   page   contentType= "text/html;   charset=gb2312 "   language= "java "   import= "java.sql.* "   errorPage= " "   %>
<%@   include   file= "dbcon.jsp "   %>

<html>
<head>
<meta   http-equiv= "Content-Type "   content= "text/html;   charset=gb2312 ">
<title> 删除 </title>
</head>
<%  
        int   id_danwei=Integer.parseInt(request.getParameter( "id_danwei "));

        if(id_danwei!=0){
        try{
                String   sql_renyuan= "select   id_renyuan   from   renyuan   where   id_danwei= "+id_danwei;
                Statement   stmt_renyuan=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
                ResultSet   rs_renyuan=stmt_renyuan.executeQuery(sql_renyuan);
                rs_renyuan.next();
                int   id_renyuan=rs_renyuan.getInt(1);
                rs_renyuan.close();
                stmt_renyuan.close();

                if(id_renyuan==0){

        try{
        String   sql= "delete   from   danwei   where   id_danwei= "+id_danwei;
        Statement   stmt=conn.createStatement();
        stmt.executeUpdate(sql);
                                out.print( "删除成功! ");
stmt.close();
       
        }catch(SQLException   e){
        out.print( "e:   "+e);
        }
                       
                }
                else{
                        out.print( "单位数据不能删除,还有所属人员,请先从人员表中删除所属人员! ");
        out.print( " <a   href= 'danwei_show.jsp '> 返回 </a> ");
                }

                conn.close();


        }catch(SQLException   e1){
        out.print( "e1:   "+e1);
        }
        }
        else{
        out.print( "单位ID错误,找不到数据! ");
        }


%>
<body>
</body>
</html>

结果报错:e1:   java.sql.SQLException:   结果集已耗尽  
同样的代码在mysql数据库中就可以执行,不知应该怎样解决,盼望各位高手指点,谢谢!

[解决办法]
rs_renyuan.next();
int id_renyuan=rs_renyuan.getInt(1);

如果查出来为空怎么办?而且貌似取第一个不是取1吧
改成
int id_renyuan;
if(rs_renyuan.next()) {
id_renyuan = rs_renyuan.getInt(0);
}
试下
[解决办法]
首先,第一步,请把 conn.close();放到finally中,
如下:
finally{
conn.close();
conn = null;
stmt.close();
stmt = null;
}

这样会大大减少你出此类的问题概率。
接着如果还有问题再进行研究。

热点排行