1Z0-809 practice question 21 of 25

Given: Item table • ID, INTEGER: PK • DESCRIP, VARCHAR(100) • PRICE, REAL • QUANTITY< INTEGER And given the code fragment: 9. try { 10.Connection…

Full set: 196 questions
Given: Item table • ID, INTEGER: PK • DESCRIP, VARCHAR(100) • PRICE, REAL • QUANTITY< INTEGER And given the code fragment: 9. try { 10.Connection conn = DriveManager.getConnection(dbURL, username, password); 11. String query = “Select * FROM Item WHERE ID = 110”; 12. Statement stmt = conn.createStatement(); 13. ResultSet rs = stmt.executeQuery(query); 14.while(rs.next()) { 15.System.out.println(“ID:“ + rs.getInt(“Id”)); 16.System.out.println(“Description:“ + rs.getString(“Descrip”)); 17.System.out.println(“Price:“ + rs.getDouble(“Price”)); 18. System.out.println(Quantity:“ + rs.getInt(“Quantity”)); 19.} 20. } catch (SQLException se) { 21. System.out.println(“Error”); 22. } Assume that: The required database driver is configured in the classpath. The appropriate database is accessible with the dbURL, userName, and passWord exists. The SQL query is valid. What is the result?
Answer options
A: An exception is thrown at runtime.
B: Compilation fails.
C: The code prints Error.
D: The code prints information about Item 110. Correct answer
Correct Answer:
D. The code prints information about Item 110.
Explanation

The code will successfully connect to the database and execute the query. The ResultSet.getXXX(String columnLabel) methods are specified to be case-insensitive when looking up column names. Therefore, rs.getInt("Id") will correctly map to the ID column, rs.getString("Descrip") to DESCRIP, and so on. Assuming a record with ID = 110 exists in the Item table, the while(rs.next()) loop will execute, and the details of that item will be printed to the console. The code is syntactically correct and uses the JDBC API as intended.

Why Incorrect

A. No exception is expected, as the use of case-insensitive column labels in getXXX methods is standard JDBC behavior.
B. The code is syntactically valid and will compile, assuming the missing quote before "Quantity:" is a transcription error in the question.
C. The "Error" message is only printed if a SQLException is caught. The operations shown are standard and should not cause an exception.

References

- JDBC™ 4.2 API Specification. (2014). JSR-221, Maintenance Release 2. Section 15.2.2, "Retrieving Column Values from Result Sets," states: "A column may be referenced by its name or by its index. The column name is case-insensitive."

- Oracle. (2017). Java SE 8 API Documentation, java.sql.ResultSet interface. While the API doc itself does not explicitly state case-insensitivity, it is a requirement defined by the overarching JDBC Specification that compliant drivers must follow.

Scroll to Top