Subject Re: [Firebird-Java] Execute Multiple Queries on a Single Connection
Author Roman Rokytskyy
> Is it possible Execute Multiple Queries on a Single Connection? I mean
> use the two PrepareStatement at the same time within single
> connection?

Sure. But you have to set the auto-commit property to false. The JDBC
specification requires explicitly to close the result set of one statement
before the another is executed in auto-commit mode.

> Here is my code:
> =====================================
> try {
> Connection con = getConnection(); // Get the jayBird connection

Add

con.setAutoCommit(false);

> try {

> String sqlModules = "Select * from modules";
> String sqlStudentTest = "select * from join student_test"

> ResultSet rModules, rStudentTest;

> PreparedStatement pstmt;
> pstmt = con.prepareStatement(sqlModules);
> rModules = pstmt.executeQuery();

> PreparedStatement pstmt2;
> pstmt2 = con.prepareStatement(sqlStudentTest);

> // Error: Exception: The result set is closed
> while (rModules.next()) {
> String title = rModules.getString("Title");

> pstmt2.setString(1, rModules.getString("Module"));
> rStudentTest = pstmt2.executeQuery();
> while (rStudentTest.next()) {
> .....
> }
> rStudentTest.close();

And then here:

con.commit();

> }

} catch(SQLException ex) {

con.rollback();

> } finally {
> con.close();
> }
> } catch (SQLException exception) {
> System.err.print("Exception: ");
> System.err.println(exception.getMessage());
> }

> =====================================

> Looks like once call 'pstmt2 = con.prepareStatement(sqlStudentTest);',
> the rModules ResultSet has been closed.

Yes, that is correct - the JDBC specification requires this.

Roman