java.sql.SQLException:用尽的结果集错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23219733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 21:24:56  来源:igfitidea点击:

java.sql.SQLException: Exhausted Resultset error

javasqljdbcresultset

提问by user2335123

I'm facing the problem with this exception java.sql.SQLException: Exhausted Resultsetin following code. I'm sure my query returns only one value. Even If I don't use rs.next();it throws the error java.sql.SQLException: ResultSet.next was not called. Could you please help?

我在下面的代码中遇到了这个异常java.sql.SQLException: Exhausted Resultset的问题。我确定我的查询只返回一个值。即使我不使用rs.next(); 它抛出错误 java.sql.SQLException: ResultSet.next was not called。能否请你帮忙?

FYI, I'm using the another result set in main where this menthod from another class is called. will it affect?

仅供参考,我在 main 中使用另一个结果集,其中调用了另一个类的方法。会影响吗?

Thanks

谢谢

public static String getdispname(Connection conn, String resname) 
throws SQLException, Exception {

            //String resname = "";
            String returnValue = "";
            String querystring = "";

            //Query to select the displayname from resid

            querystring += "select distinct display_name";
            querystring += " from cust_rally_team_member";
            querystring += " where display_name like '%"+ resid +"%'";

            // Create select statement
            Statement stmt = conn.createStatement();

            try {
                // Execute statement
                ResultSet rs = stmt.executeQuery(querystring);
                if (rs!= null) { 
            while (rs.next()) {
            returnValue = rs.getString("display_name");
            } catch (SQLException ex) {
                throw new SQLException(ex);
            } catch (Exception ex) {
                throw new Exception(ex);
            }
            // Close statement
            finally {
                stmt.close();
            }

            return returnValue;

采纳答案by J?cob

Try as

尝试作为

if (rs! = null) { 
 while (rs.next()) {
  returnValue = rs.getString("display_name");
}
......

回答by Paul

Try:

尝试:

returnValue = rs.next() ? rs.getString("display_name") : null;

You don't need to check if the rsis null. It won't be - assuming the executeQuery()returned rather than raising an exception. (Though the returned result set might have 0 rows).

您不需要检查它是否rs为空。它不会 - 假设executeQuery()返回而不是引发异常。(虽然返回的结果集可能有 0 行)。

You also don't need to loop over the result set, assuming you reallyknow that you expect back a single row. (Though, given the query, that seems unlikely.)

您也不需要遍历结果集,假设您真的知道您期望返回单行。(不过,鉴于查询,这似乎不太可能。)

回答by Karibasappa G C

use by your modification like below

通过您的修改使用,如下所示

if (rs != null && rs.first()) {
    do {
        returnValue = rs.getString(("display_name");
    } while (rs.next());
}

回答by Sachindra N. Pandey

I am using Oracle 10g database , i found same error "java.sql.SQLException: Exhausted Resultset error". I just grant permission in database and solved my probblem.

我正在使用 Oracle 10g 数据库,我发现同样的错误“java.sql.SQLException: Exhausted Resultset error”。我只是在数据库中授予权限并解决了我的问题。

SQL> grant insert,update,delete on "table-name" to "database_name";

SQL> 将“表名”上的插入、更新、删除授予“数据库名”;