What is the result of compiling and executing this code fragment?

Given the code fragment:

String query = “SELECT ID FROM Employee”;

try (Statement stmt = conn.createStatement()) {

ResultSet rs = stmt.executeQuery(query);

stmt.executeQuery(“SELECT ID FROM Customer”); // Line ***

while (rs.next()) {

// process the results

System.out.println (“Employee ID: ” + rs.getInt(“ID”));

}

} catch (Exception e) {

System.err.println (“Error”);

}

Assume that the SQL queries return records.
What is the result of compiling and executing this code fragment?

Given the code fragment:

String query = “SELECT ID FROM Employee”;

try (Statement stmt = conn.createStatement()) {

ResultSet rs = stmt.executeQuery(query);

stmt.executeQuery(“SELECT ID FROM Customer”); // Line ***

while (rs.next()) {

// process the results

System.out.println (“Employee ID: ” + rs.getInt(“ID”));

}

} catch (Exception e) {

System.err.println (“Error”);

}

Assume that the SQL queries return records.
What is the result of compiling and executing this code fragment?

A.
The program prints employee IDs

B.
The program prints customer IDs

C.
The program prints Error

D.
Compilation fails on line***

Explanation:
The program compiles and runs fine. Both executeQuery statements will run. The first executeQuery statement (ResultSet rs = stmt.executeQuery(query);) will set the rs Resultset. It will be usedin the while loop. EmployIDs will be printed.

Note:Executes the given SQL statement, which returns a single ResultSet object.
Parameters:
sql – an SQL statement to be sent to the database, typically a static SQL SELECT statement Returns:

a ResultSet object that contains the data produced by the given query; never null



Leave a Reply 9

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


Marian

Marian

The program like it is prints nothing. Something is missing here. I guess should be:
rs=stmt.executeQuery(“SELECT ID FROM Customer”); // Line ***

In any case answer A is impossible.

Serg

Serg

Error
java.sql.SQLException: Operation not allowed after ResultSet closed
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:910)
at com.mysql.jdbc.ResultSet.checkClosed(ResultSet.java:666)
at com.mysql.jdbc.ResultSet.next(ResultSet.java:7274)
at test1.F41.main(F41.java:50)

Serg

Serg

Correct is – C. The program prints Error

Marian

Marian

next() should throw the exception, but probably it depends on the implementation of the driver. With Oracle there is no error

Bahaa Khateib

Bahaa Khateib

No you have some configuration errors, the driver definition is sliced here, but doesn’t mean that the program is nit working

B

B

Answer is A
Line *** returns a ResultSet which is not been passed to any variable.

Tim

Tim

C. From Java ResultSet API: A ResultSet object is automatically closed when the Statement object that generated it is closed, re-executed, or used to retrieve the next result from a sequence of multiple results.

Bahaa Khateib

Bahaa Khateib

Answer is C
Exception in thread “main” com.microsoft.sqlserver.jdbc.SQLServerException: The result set is closed.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.checkClosed(SQLServerResultSet.java:372)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.next(SQLServerResultSet.java:994)
at main.Q40.main(Q40.java:25)

used code “SQL server contains valid data”

Connection conn = DriverManager.getConnection(“jdbc:sqlserver://localhost;user=sa;password=sa;database=bah”);
Statement sta = conn.createStatement();
String Sql = “select * from CUSTOMER”;
ResultSet re = sta.executeQuery(Sql);
Sql = “select * from EMPLOYEE”;
sta.executeQuery(Sql);

while(re.next()){
System.out.println(re.getString(“NAME”));
System.out.println(re.getInt(“ID”));

}
conn.close();

Dylan

Dylan

C. The second query, even though it is not assigned to any variable, closes the resultSet pointed to by rs.