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

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.

dbURL, userName, and passWord exists.

The appropriate database is accessible with the

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

The -

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

    Correct Answer: C

    The program will print 'Error'. The reason is that on line 13, the `executeQuery` method of the `Statement` object is called again, which will cause the previous `ResultSet` (`rs`) to be automatically closed. When the code then attempts to iterate over `rs` starting on line 14, a `SQLException` will be thrown because the `ResultSet` is no longer valid. This exception is caught by the catch block on line 18, which prints 'Error'.

Discussion
M_JawadOption: C

it will print Error The 13th line of the program uses the executeQuery method of the Statement object again, which will cause the previous ResultSet to be automatically closed, so the 14th line of the program will throw a SQLException.

asdfjhfgjuaDCVOption: C

C is the answer

steefaandOption: C

C is correct, as ResultSet is closed once the new statement is run and it throws exception.

iSnoverOption: C

Answer is C. The 13th line of the program uses the executeQuery method of the Statement object again, which will cause the previous ResultSet to be automatically closed, so the 14th line of the program will throw a SQLException.

WilsonKKerllOption: C

Answer is C.

UchihaOption: C

Answer is C

mevltOption: B

The answer is B(tested)

mevlt

Second statement overrides.

adnano1234Option: A

A. The program prints employee IDs.

adnano1234

Sorry, the program prints Error as explained by M_Jawad