Result Set Methods to Extract Column Values of the Current Row
The following get methods of the ResultSet interface can be used to extract the column values of the current row indicated by the cursor in the result set. The column index or the column label can be passed as a parameter to designate the appropriate column in the table. The CallableStatement interface provides analogous getXXX() methods to retrieve values of OUT parameters (p. 1530).
Selected get methods from the ResultSet interface to extract column values of the current row are shown below:
String getString(int columnIndex)
String getString(String columnLabel)
Retrieve the value of the column as a String.
int getBoolean(int columnIndex)
int getBoolean(String columnLabel)
Retrieve the value of the column as a boolean.
int getInt(int columnIndex)
int getInt(String columnLabel)
Retrieve the value of the column as an int.
double getLong(int columnIndex)
double getLong(String columnLabel)
Retrieve the value of the column as a long.
double getDouble(int columnIndex)
double getDouble(String columnLabel)
Retrieve the value of the column as a double.
BigDecimal getBigDecimal(int columnIndex)
BigDecimal getBigDecimal(String columnLabel)
Retrieve the value of the column as a BigDecimal.
Date getDate(int columnIndex)
Date getDate(int columnLabel)
Retrieve the value of the column as a Date.
<T> T getObject(int columnIndex, Class<T> type)
<T> T getObject(int columnLabel, Class<T> type)
Retrieve the value of the column as an object of the indicated Java type.
Example 24.5 Processing a ResultSet
package dbDemo;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.time.Duration;
import static java.lang.System.out;
public class ResultSetProcessing {
public static void main(String[] args) {
final String jdbcURL = “jdbc:derby:musicDB”;
final String sql = “select * from compositions where duration > ?”; // (1)
try (var connection = DriverManager.getConnection(jdbcURL); // (2)
var pStatement = connection.prepareStatement(sql);) { // (3)
pStatement.setInt(1, 0); // (4)
var resultSet = pStatement.executeQuery();
try (resultSet) { // (5)
while (resultSet.next()) { // (6)
String isrc = resultSet.getString(1); // (7)
String title = resultSet.getObject(2, String.class); // (8)
int duration = resultSet.getInt(“duration”); // (9)
out.println(“[” + isrc + “, ” + title + “, ” + duration + “]”); // (10)
}
} // Closes the result set.
} catch (SQLException e) { // (11)
e.printStackTrace();
} // Closes the prepared statement and the connection.
}
}
Probable output from the program:
[ushm91736697, Vacation, 231]
[ushm91736698, Rage, 308]
[ushm91736699, Why Don’t, 178]
Example 24.5 illustrates the main steps in the interaction between a Java application and a relational database. The example uses the musicDB database that has been used so far to illustrate salient features of programming this interaction. The numbered comments below correspond to the numbered code lines in Example 24.5.
- Define the SELECT statement to execute, having one marker parameter for the second operand of the boolean expression.
- Use the try-with-resources statement to declare the resources and to close the connection and the prepared statement afterward. Obtain the connection to the database.
- Create the prepared statement in the header of the try-with-resources statement.
- Substitute the marker parameter with a value in the SELECT statement at (1).
- Use the nested try-with-resources statement to close the result set after processing it. The prepared statement is executed in the header of the try-with-resources statement, returning a result set. Any exception thrown will be propagated to the catch clause at (11) of the outer try-with-resources statement.
- The while loop processes the rows in the result set.
- Extract the column values of the current row denoted by the result set cursor by calling the appropriate get methods of the result set. Extract a string (isrc) from column 1 using the getString() method.
- Extract a string (title) from column 2 using the getObject() method.
- Extract an int value (duration) by column name using the getInt() method.
- Print the column values of the current row.
- Use the catch clause of the outer try-with-resources statement to catch any SQLException.