java 连接正忙于另一个 hstmt 的结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15019151/
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-10-31 18:13:47  来源:igfitidea点击:

Connection is busy with results for another hstmt

java

提问by Brainser

I am trying to iterate ResultSet values to insert query in while loop. The code snippet is as follows:

我正在尝试迭代 ResultSet 值以在 while 循环中插入查询。代码片段如下:

    String sel="select roll_no from Nursery";
    rs=stmt.executeQuery(sel);
    stmt1=con.createStatement();
    int aa;
    while(rs.next())
    {
        aa=rs.getInt(1);
        stmt1.executeUpdate("insert into [Nursery_FirstTerminal_marks](roll_no)values("+aa+")");
        //stmt.close();
    }

    JOptionPane.showMessageDialog(null, "Data has been inserted");
}
    catch(Exception e)
    {
        System.out.println(e);
        // JOptionPane.showMessageDialog(null, e);
    }

but the below given error is thrown.

但是抛出了下面给出的错误。

java.sql.SQLException: [Microsoft][ODBC SQL Server Driver]Connection is busy with results for another hstmt

Can you please suggest how to get out of it..

你能建议如何摆脱它吗?

回答by LGAP

This is caused by you have a opening result set open against the same connection. For example, you execute a SqlCommand to select all rows from a table, while the result set is not drained, you try to execute another SqlCommand using the same connection, you will hit this error message.

这是由于您有一个针对同一连接打开的打开结果集。例如,您执行一个 SqlCommand 来选择一个表中的所有行,而结果集没有排空,您尝试使用相同的连接执行另一个 SqlCommand,您将遇到此错误消息。

To solve this, you have two choices:

要解决这个问题,您有两种选择:

a. Make sure you read the rest data from the pending result set before you send the next SqlCommand.

一个。确保在发送下一个 SqlCommand 之前从挂起的结果集中读取其余数据。

b. Use MARS (Multiple Active ResultSet) connection setting to enable multiple active result set in a connection.

湾 使用 MARS (Multiple Active ResultSet) 连接设置在一个连接中启用多个活动结果集。

回答by Jarekczek

In C# there is a possibility to Disposea Commandobject after using it. Autogarbage for the Commandobject is not enough. Try to free it explicitly. Maybe Statement.close()is what you need. Just guessing.

在C#存在这样的可能性,以Dispose一个Command使用后的对象。Command对象的自动垃圾是不够的。尝试明确释放它。也许Statement.close()正是你所需要的。只是猜测。