Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connection is busy with results for another hstmt

Tags:

java

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

    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..

like image 640
Brainser Avatar asked Feb 22 '13 07:02

Brainser


1 Answers

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.

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.

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

like image 66
LGAP Avatar answered Sep 27 '22 22:09

LGAP