Which object is valid after the try block runs?

Given this code fragment:
<code>
ResultSet rs = null;
try (Connection conn = DriverManager. getConnection (url) ) {
Statement stmt = conn.createStatement();
rs stmt.executeQuery(query);
//-.. other methods }
} catch (SQLException se) {
System.out.println (“Error”);
}
</code>

Which object is valid after the try block runs?

Given this code fragment:
<code>
ResultSet rs = null;
try (Connection conn = DriverManager. getConnection (url) ) {
Statement stmt = conn.createStatement();
rs stmt.executeQuery(query);
//-.. other methods }
} catch (SQLException se) {
System.out.println (“Error”);
}
</code>

Which object is valid after the try block runs?

A.
The Connection object only

B.
The Statement object only

C.
The Result set object only

D.
The Statement and Result Set object only

E.
The connection, statement, and ResultSet objects

F.
Neither the Connection, Statement, nor ResultSet objects

Explanation:
Generally, JavaScript has just 2 levels of scope: global and function. But, try/catch is an exception (no punn intended). When an exception is thrown and the exception object gets a variable assigned to it, that object variable is only available within the “catch” section and is destroyed as soon as the catch completes.



Leave a Reply 2

Your email address will not be published. Required fields are marked *


ahmed abbas

ahmed abbas

answer ::: F

The following code will throw exception :::
java.sql.SQLException: Operation not allowed after ResultSet closed

——————–

String query = “select * from auth_user”;
String url = “jdbc:mysql://localhost:3306/marketplace”;

ResultSet rs = null;
try (Connection conn = DriverManager.getConnection(url, “root”, “”);)
{
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(query);
// -.. other methods }
} catch (SQLException se) {
System.out.println(“Error”);
}

rs.next();