Chapter TWENTY-NINE
JDBC API


Exam Objectives

Describe the interfaces that make up the core of the JDBC API including the Driver, Connection, Statement, and ResultSet interfaces and their relationship to provider implementations.
Identify the components required to connect to a database using the DriverManager class including the JDBC URL.
Submit queries and read results from the database including creating statements, returning result sets, iterating through the results, and properly closing result sets, statements, and connections.

Answers

1. The correct answer is B.
If the result set is empty, next() returns false, so nothing is printed.


2. The correct answer is C.
If the argument of absolute() is negative, the cursor is positioned starting from the end of the result set. Since last() moves the cursor to the last element of the result set, this and absolute(-1) are equivalent.


3. The correct answer is D.
The first object you have to close is ResultSet, then Statement, and finally Connection.


4. The correct answer is B.
execute() returns a boolean value, true for SELECT and false for other statements.


5. The correct answer is D.
Option A is invalid. The method getInteger() doesn't exist (the correct one is getInt()).
Option B is invalid. The string argument must be the name of the column.
Option C is invalid. In JDBC, indexes start at 1.
Option D is valid. rs.getBoolean(1) can be a valid way to get the value of the first column of a row.