简体   繁体   English

java.sql.SQLException:Exhausted Resultset错误

[英]java.sql.SQLException: Exhausted Resultset error

I'm facing the problem with this exception java.sql.SQLException: Exhausted Resultset in following code. 我遇到了这个异常的问题java.sql.SQLException:以下代码中的Exhausted Resultset I'm sure my query returns only one value. 我确定我的查询只返回一个值。 Even If I don't use rs.next(); 即使我不使用rs.next(); it throws the error java.sql.SQLException: ResultSet.next was not called . 它抛出错误java.sql.SQLException:未调用ResultSet.next Could you please help? 能否请你帮忙?

FYI, I'm using the another result set in main where this menthod from another class is called. 仅供参考,我正在使用另一个来自另一个类的menthod的主要结果集。 will it affect? 会影响吗?

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;

Try as 试试吧

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

Try: 尝试:

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

You don't need to check if the rs is null. 您无需检查rs是否为null。 It won't be - assuming the executeQuery() returned rather than raising an exception. 它不会 - 假设executeQuery()返回而不是引发异常。 (Though the returned result set might have 0 rows). (尽管返回的结果集可能有0行)。

You also don't need to loop over the result set, assuming you really know that you expect back a single row. 您也不需要遍历结果集,假设您确实知道您希望返回单行。 (Though, given the query, that seems unlikely.) (虽然,鉴于查询,这似乎不太可能。)

use by your modification like below 使用您的修改,如下所示

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

I am using Oracle 10g database , i found same error "java.sql.SQLException: Exhausted Resultset error". 我使用的是Oracle 10g数据库,我发现了同样的错误“java.sql.SQLException:Exhausted Resultset error”。 I just grant permission in database and solved my probblem. 我只是在数据库中授予权限并解决了我的问题。

SQL> grant insert,update,delete on "table-name" to "database_name"; SQL>将“table-name”上的insert,update,delete赋予“database_name”;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM