当前位置: 首页 > 知识库问答 >
问题:

如何解决抛出的错误SQL异常:java。sql。SQLException:结果集关闭后不允许操作[重复]

段干长恨
2023-03-14

我正在尝试运行以下代码,但出现错误:

引发SQL异常:java。sql。SQLException:结果集关闭后不允许操作。

如何解决此错误?我的申请需要两个结果集。

public static void main(String[] args) {
        String connectionUrl = "jdbc:mysql://127.0.0.1:3306/test";
        String dbUser = "root";
        String dbPwd = "Syntel#92";
        Connection conn;
        ResultSet rs, res1 = null;
        Statement stmt = null;
        int rowcount = 0;
    //  String queryString = "create table job_status_table as select j1.job_id,s1.Source_ID,s1.Source_name from JOb_list j1,SOURCE_DETAILS s1 where s1.Source_ID = j1.Source_ID";
        String queryString = "create table job_status_table as select source_id,source_name from source_details";

        String addcolumn = "alter table job_status_table add column Source_rowcount int";
        String fetchdata = "Select Source_name from job_status_table";
        try {
            conn = DriverManager.getConnection(connectionUrl, dbUser, dbPwd);
            stmt = conn.createStatement();
            // get record count from table job_status_table1
            // stmt.executeQuery("select count() from job_status_table1");
            // create table
            stmt.executeUpdate(queryString);
            System.out.println("Table created in the database");
            stmt.executeUpdate(addcolumn);
            System.out.println("alter table");
            rs = stmt.executeQuery(fetchdata);
            System.out.println("fetch data");
            while (rs.next()) {
                String table_count = null;
                String table_name = null;




                    table_name = rs.getString("Source_name");
                    System.out.println(table_name);


            //  table_name = rs.getString("Source_name");
                //System.out.println(table_name);
                //rs.close();

                    table_count = "select count(*) from " + table_name;
                //table_count = "select count(*) from " + table_name;
                //rs.close();
            //  res1 = stmt.executeQuery(table_count);

                res1 = stmt.executeQuery(table_count);
                //System.out.print(res1);
                if (res1.next()) {

                    rowcount = res1.getInt(1);//res1.getInt(1);
                    System.out.println("Result set values" + rowcount);

                } else {
                    System.out.println("value is not present in resultset");
                }


                System.out.println("Get Row Count");
                    System.out.println(table_count);
            //      int cnt = rcnt(table_count);
                String updaterow = "update job_status_table set Source_rowcount ="
                        + rowcount
                        + " where Source_name = '"
                        + table_name
                        + "'";

                System.out.println("updateoutput" +stmt.executeUpdate(updaterow));

                System.out.println("Update Complete");

            }

    /*      if (conn != null) {

                rs.close();
                res1.close();
                stmt.close();
                conn.close();
                conn = null;
            }
            */
        }
        catch (SQLException sqle) {
            System.out.println("SQL Exception thrown: " + sqle);
        }
    }
}**

共有1个答案

呼延英奕
2023-03-14

你可以试试这个:

首先在ArrayList中复制ResultSet rs并关闭它。

在更新之前迭代ArrayList并关闭res1

我不认为其他值不存在于结果集中是可达的,但如果您应该设置rowcount0

编辑

在第二次阅读参考问题后:问题是对res1stmt的重用和更新

 类似资料:
  • 我试图为通过html形式的文本框检索的查询编写一个JUnit测试。文本检索已经过测试并有效,但我的单元测试失败了。我使用了两个相关的类:QueryController和QueryControlllerTest。我一直在玩弄我在这两个类中关闭的时间和内容,并不断得到错误:结果集关闭后不允许操作。 QueryController测试。Java语言 QueryController。Java语言

  • 下面是相关的代码部分:Server. Proxy.update_allLogOut:(第65行是时括号) 服务器服务器: 主要: 我必须指出,它99%的运行时间都没有例外。。但有几次我遇到了这个例外,我不明白为什么会发生这种情况。有什么想法吗?很明显,有些地方做错了,但这里所有类似的主题都没有给出这个问题的答案。。。

  • 我正在使用Prepared语句进行我的SELECT查询。之后,我试图从结果集获取信息,并将其放入数组列表中。我正在将其放入结果集循环,但我得到了一个错误。sql。SQLException:结果集关闭后不允许操作。我试图用rs.close()关闭它,但它给了我同样的错误。这是我的方法。请帮忙!

  • 我已经为此工作了将近一天半,我似乎无法解决这个错误。我不知道为什么结果集被关闭。也许你们中的一些人可以帮我。 MySQL数据库: MySQLHandler MySQLDonation 异常发生在这里:在MySQLDonation和实际堆栈跟踪的同时循环中: 有了这些信息,让我说这确实有效,我得到了我的信息和游戏中没有的东西,但它会重复,就像用户永远不会从查询中删除,所以它会给他们无限的奖励。如果您

  • 我是java和SQL新手,我在重复一个我不知道如何避免的问题: 假设我想创建两个执行查询,一个在getRequest estsFromDB方法中的另一个内部,我创建第一个执行查询,在第二个方法中,我创建第二个执行查询: 第二个功能: 在第二个循环中,我得到了一个SQLException:“ResultSet关闭后不允许操作”,我尝试了:最后用result关闭结果。close()但我也得到异常nul

  • 单击按钮时,此代码显示此错误消息: 结果集关闭后不允许操作 代码: