nerdexam
Oracle

1Z0-819 · Question #140

Given: var sql = "SELECT * FROM students"; try (var conn = DriverManager.getConnection(args[0]); var ps = conn.prepareStatement(sql)) { ResultSet rs = ps._ ____(); while (rs.next()) { int i = ps._…

The correct answer is C. executeQuery, execute, executeUpdate. Option C works because the two typed variable assignments in the code rigidly constrain which methods can fill each blank: ResultSet rs = ps.executeQuery() compiles because executeQuery() is the only PreparedStatement method that returns a ResultSet, and int i =…

Database Applications with JDBC

Question

Given: var sql = "SELECT * FROM students"; try (var conn = DriverManager.getConnection(args[0]); var ps = conn.prepareStatement(sql)) { ResultSet rs = ps._ ___(); while (rs.next()) { int i = ps. ____(); } } Which of the combinations can be filled in the blanks in order to make the code compile?

Options

  • Aexecute, executeUpdate, executeQuery
  • BexecuteUpdate, executeQuery, execute
  • CexecuteQuery, execute, executeUpdate
  • DexecuteUpdate, execute, executeQuery
  • Eexecute, executeQuery, executeUpdate
  • FexecuteQuery, executeUpdate, execute

How the community answered

(52 responses)
  • A
    10% (5)
  • B
    2% (1)
  • C
    85% (44)
  • D
    4% (2)

Explanation

Option C works because the two typed variable assignments in the code rigidly constrain which methods can fill each blank: ResultSet rs = ps.executeQuery() compiles because executeQuery() is the only PreparedStatement method that returns a ResultSet, and int i = ps.executeUpdate() compiles because executeUpdate() returns an int representing the row count. The execute() method in C is the leftover option - it returns boolean and cannot be assigned to either ResultSet or int, confirming it has no valid role here. Options A, B, D, and E are immediately eliminated because they place either execute (returns boolean) or executeUpdate (returns int) as the first method, and neither can be assigned to ResultSet. Option F (executeQuery, executeUpdate, execute) is the common trap - it starts correctly with executeQuery, but the ordering places execute (boolean) as the candidate for blank 2 rather than executeUpdate, making it fail to assign to int i.

Memory tip: Think Q → RS → int - Query gives you a ResultSet; Update gives you an int (rows changed); Execute gives you a boolean (anything else). Match the return type to the variable type, and the blanks fill themselves.

Topics

#JDBC PreparedStatement methods#executeQuery/executeUpdate return types#ResultSet#SQL execution

Community Discussion

No community discussion yet for this question.

Full 1Z0-819 Practice