public interface JdbcQuery<T> extends AutoCloseable
This API uses the builder pattern to allow a query to be easily configured and executed. It supports queries that return many rows as well as those that return a single row. The API is designed to allow a fluent specification of the query configuration.
An instance of this type is created using the query
or queryForType method on the
JdbcOperations interface. The user of this interface must configure
at least two characteristics:
using
method
Additionally, the query can be configured for single execution (default)
or repeated execution. When a query is configured for repeated execution
(using repeatedly()), the underlying statement and connection
objects remain open after a result is retrieved, allowing the query to be
executed again with different parameter values. A query that is configured
for repeated execution must be closed when it is no longer needed, by
invoking the close() method explicitly or by enclosing it in a
try-with-resources construct.
After a query has been fully configured, a result can be retrieved using either
retrieveList(Parameter...) for a query that returns multiple
rows, OR
retrieveValue(Parameter...) for a query that returns exactly
one row
Examples:
Retrieving a list of objects that correspond to the rows returned by
a query using a RowMapper:
// A mapper that maps column values of a person row to a Person object
RowMapper<Person> personMapper = new RowMapper<Person>() { ... };
List<Person> results = sqlTemplate.queryForType(Person.class)
.using("SELECT * FROM person ORDER BY name")
.mappingRowsWith(personMapper)
.retrieveList();
Retrieving a list of values for a given column for all matching rows:
List<String> names = sqlTemplate.queryForType(String.class)
.using("SELECT * FROM person ORDER BY name")
.extractingColumn("name")
.retrieveList();
Retrieving a single object using a RowMapper:
// A mapper that maps column values of a person row to a Person object
RowMapper<Person> personMapper = new RowMapper<Person>() { ... };
long id = 3; // ID of the person to retrieve
Person person = sqlTemplate.queryForType(Person.class)
.using("SELECT * FROM person WHERE id = ?")
.mappingRowsWith(personMapper)
.retrieveValue(Parameter.with(id));
Retrieving a single value of a column:
int averageAge = sqlTemplate.queryForType(int.class)
.using("SELECT AVG(age) FROM person")
.extractingColumn()
.retrieveValue();
Repeatedly executing a query with different parameters:
try (JdbcQuery<String> query = sqlTemplate.queryForType(String.class)
.using("SELECT name FROM person WHERE name LIKE ?")
.extractingColumn()
.repeatedly()) {
System.out.format("matching names: %s",
query.retrieveList("%en%"));
System.out.format("matching names: %s",
query.retrieveList("%sh%"));
}
Processing the returned result set using a ResultSetHandler:
sqlTemplate.query()
.using("SELECT * FROM person ORDER BY id")
.handlingResultWith(new ResultSetMapper<Void>() {
public void handleResult(ResultSet rs) throws SQLException {
while (rs.next()) {
exporter.exportPerson(rs.getLong("id"), rs.getLong("name"));
}
return null;
}
})
.retrieveValue();
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Closes the JDBC resources associated with this query.
|
void |
execute(Parameter... parameters)
Executes the query, processing the result set with the configured handler.
|
JdbcQuery<T> |
extractingColumn()
Configures this query to extract the value of the first column.
|
JdbcQuery<T> |
extractingColumn(int index)
Configures this query to extract the value of the given column.
|
JdbcQuery<T> |
extractingColumn(String label)
Configures this query to extract the value of the given column.
|
JdbcQuery<T> |
handlingResultWith(ResultSetHandler<T> handler)
Configures this query to use the given result set handler.
|
JdbcQuery<T> |
mappingRowsWith(RowMapper<T> rowMapper)
Configures this query to use the given row mapper.
|
JdbcQuery<T> |
repeatedly()
Configures this query for repeated execution.
|
List<T> |
retrieveList(Parameter... parameters)
Executes the query, retrieving the list of values for all matching rows.
|
T |
retrieveValue(Parameter... parameters)
Executes the query, retrieving a value representing the matching row
|
JdbcQuery<T> |
using(SQLSource source)
Configures this query to execute the given SQL statement.
|
JdbcQuery<T> |
using(String sql)
Configures this query to execute the given SQL statement.
|
JdbcQuery<T> using(String sql)
sql - the SQL statement to executeJdbcQuery<T> using(SQLSource source)
source - source for the SQL statement to executeJdbcQuery<T> handlingResultWith(ResultSetHandler<T> handler)
Invoking this method replaces any existing configured row mapper or column extractor with the given result set handler.
handler - result set extractorJdbcQuery<T> extractingColumn()
Invoking this method replaces any existing configured result set extractor or row mapper with the given column extractor.
JdbcQuery<T> extractingColumn(int index)
Invoking this method replaces any existing configured result set extractor or row mapper with the given column extractor.
index - column index (index values start at 1)JdbcQuery<T> extractingColumn(String label)
Invoking this method replaces any existing configured result set extractor or row mapper with the given column extractor.
label - column labelJdbcQuery<T> mappingRowsWith(RowMapper<T> rowMapper)
Invoking this method replaces any existing configured result set extractor or row mapper with the given column extractor.
rowMapper - row mapperJdbcQuery<T> repeatedly()
A query that is configured as repeatable must be explicitly closed by
the caller using close() when no longer needed.
List<T> retrieveList(Parameter... parameters)
parameters - values for query placeholdersT that were extracted/mapped by
this queryT retrieveValue(Parameter... parameters)
parameters - values for query placeholdersT that was extracted/mapped by this querySQLNoResultException - if no row was matched by this querySQLNonUniqueResultException - if more than one row was matched by
this queryvoid execute(Parameter... parameters)
This method is a synonym for retrieveValue(Parameter...), but
does not return the result produced by the query. It is typically used
when the query is configured with a ResultSetHandler that has a
void return type.
parameters - values for query placeholdersvoid close()
After a query is closed, its retrieval methods may not be subsequently invoked.
close in interface AutoCloseableCopyright © 2014–2015 Carl Harris, Jr. All rights reserved.