【问题标题】:java.sql.SQLException: Exhausted Resultsetjava.sql.SQLException:用尽的结果集
【发布时间】:2011-03-30 22:37:13
【问题描述】:

我收到错误 java.sql.SQLException: Exhausted ResultSet 来针对 Oracle 数据库运行查询。该连接是通过 Websphere 中定义的连接池进行的。执行的代码如下:

if (rs! = null) (
    while (rs.next ()) (
        count = rs.getInt (1);
    )
)

我注意到结果集中包含数据(rs.next ())

谢谢

【问题讨论】:

  • 读取结果集时语句或连接是否关闭?
  • @Arne:那宁愿抛出SQLException: ResultSet is closed
  • 以后添加stacktrace并点线。在您在问题中发布的代码中,看到已接受的答案绝对不是。您应该更多地练习解释堆栈跟踪。我删除了我的答案。
  • 是的,绝对不在代码中,因为我想出来了 :-) 我一直看到这个错误是由同一个问题引起的。

标签: java sql oracle jdbc


【解决方案1】:

我在处理结果集后尝试访问列值时看到此错误。

if (rs != null) {
  while (rs.next()) {
    count = rs.getInt(1);
  }
  count = rs.getInt(1); //this will throw Exhausted resultset
}

希望对你有帮助:)

【讨论】:

  • 简单明了,谢谢
  • 是否会为其他数据库(如mysql、sql server等)创建相同的异常?
  • 我不知道。可能是。我想它取决于数据库供应商:-)
  • '! =' 是不允许的:)
  • 谢谢。改变了我的答案。
【解决方案2】:

试试这个:

if (rs != null && rs.first()) {
    do {
        count = rs.getInt(1);
    } while (rs.next());
}

【讨论】:

    【解决方案3】:

    如果您将结果集重置为顶部,使用 rs.absolute(1) 您不会得到用尽的结果集。

    while (rs.next) {
        System.out.println(rs.getString(1));
    }
    rs.absolute(1);
    System.out.println(rs.getString(1));
    

    您也可以使用 rs.first() 代替 rs.absolute(1),它的作用相同。

    【讨论】:

      【解决方案4】:

      当数据库没有针对特定条件返回任何记录并且我尝试访问 rs.getString(1);我收到此错误“用尽结果集”。

      在问题出现之前,我的代码是:

      rs.next();
      sNr= rs.getString(1);
      

      修复后:

      while (rs.next()) {
          sNr = rs.getString(1);
      }
      

      【讨论】:

        【解决方案5】:

        在 while 循环之外使用 ResultSet 时会发生此异常。请将与 ResultSet 相关的所有处理保留在 While 循环内。

        【讨论】:

          【解决方案6】:

          这通常发生在 stmt 被重用但期望不同的 ResultSet 时,尝试创建一个新的 stmt 并执行Query。它为我修好了!

          【讨论】:

            【解决方案7】:

            错误背后的问题:如果您尝试访问 Oracle 数据库,则在事务成功之前您将无法访问插入的数据,并且要完成事务,您必须触发 commit将数据插入表后进行查询。因为Oracle数据库默认不开启自动提交模式。

            解决方法:

            转到 SQL PLUS 并按照以下查询..

            SQL*Plus: Release 11.2.0.1.0 Production on Tue Nov 28 15:29:43 2017
            
            Copyright (c) 1982, 2010, Oracle.  All rights reserved.
            
            Enter user-name: scott
            Enter password:
            
            Connected to:
            Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
            With the Partitioning, OLAP, Data Mining and Real Application Testing options
            
            SQL> desc empdetails;
             Name                                      Null?    Type
             ----------------------------------------- -------- ----------------------------
             ENO                                                NUMBER(38)
             ENAME                                              VARCHAR2(20)
             SAL                                                FLOAT(126)
            
            SQL> insert into empdetails values(1010,'John',45000.00);
            
            1 row created.
            
            SQL> commit;
            
            Commit complete.
            

            【讨论】:

              【解决方案8】:

              请确保 res.getInt(1) 不为空。 如果可以为空,请使用 Integer count = null; 而不是 int count =0;

              Integer count = null;
                  if (rs! = null) (
                                  while (rs.next ()) (
                                      count = rs.getInt (1);
                                  )
                              )
              

              【讨论】:

                【解决方案9】:

                您收到此错误是因为您在 resultSet.next() 方法之前使用了 resultSet。

                要获得计数,只需使用这个:

                while (rs.next ()) `{ 计数 = rs.getInt (1); }

                你会得到你的结果。

                【讨论】:

                  猜你喜欢
                  • 2014-06-06
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2012-03-12
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多