Exam 1z0-809 All QuestionsBrowse all questions from this exam
Question 53

Given the code fragment:

9. Connection conn = DriveManager.getConnection(dbURL, userName, passWord);

10. String query = "SELECT id FROM Employee";

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

12. ResultSet rs = stmt.executeQuery(query);

13. stmt.executeQuery("SELECT id FROM Customer");

14. while (rs.next()) {

15. //process the results

16. System.out.println("Employee ID: "+ rs.getInt("id"));

17. }

18. } catch (Exception e) {

19. System.out.println ("Error");

20. }

Assume that:

The required database driver is configured in the classpath.

The appropriate database is accessible with the dbURL, userName, and passWord exists.

The Employee and Customer tables are available and each table has id column with a few records and the SQL queries are valid.

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

    Correct Answer: C

    The provided code snippet attempts to execute two separate SQL queries with the same Statement object. In Java, a ResultSet object is automatically closed when the associated Statement object executes another statement. After running the query to select employee IDs, the code tries to run another query to select customer IDs, which closes the first ResultSet. Attempting to access the closed ResultSet results in an SQLException, causing the catch block to execute and print 'Error'. Therefore, the correct result is that the program prints 'Error'.

Discussion
maslacOption: C

Corect answer is C. Error is: Operation not allowed after ResultSet closed. Because after executing the second query, the first one is automatically closed. Offical doc explanation: “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. ” https://docs.oracle.com/javase/8/docs/api/java/sql/ResultSet.html

karta

Absolutely!!

cnno1

You have the most words in your answer and I think you are right

Svetleto13Option: C

Correct answer is C

WilsonKKerllOption: C

Answer is C.

mevltOption: B

The answer is B(tested). It seems no matter how many times you request ResultSet it will save the last one only.

asdfjhfgjuaDCVOption: C

C is the answer

steefaandOption: C

C is correct as ResultSet is closed once the next statement is run.

r1muka5Option: C

The answer is C

HuimOption: A

Answer is A