public class QueryRunner extends Object
ResultSets.
Immutable, but only as threadsafe as the underlying Connection. Avoid sharing Connection instances across threads!ResultSetHandler| Modifier and Type | Class and Description |
|---|---|
static class |
QueryRunner.Transaction |
| Modifier and Type | Method and Description |
|---|---|
int[] |
batch(String sql,
List<List<Object>> params) |
<T> T |
batchInsert(String sql,
ResultSetHandler<T> resultSetHandler,
List<List<Object>> batchParams) |
static QueryRunner |
create(Connection connection)
try (Connection connection = DriverManager.getConnection(jdbcUrl)) {
QueryRunner queryRunner = QueryRunner.create(connection);
}
|
int |
execute(String sql,
Object... params) |
QueryRunner |
initializeWith(ConsumerWithException<Connection> initializer) |
<T> T |
insert(String sql,
ResultSetHandler<T> resultSetHandler,
Object... params) |
<T> T |
select(String sql,
ResultSetHandler<T> resultSetHandler,
Object... params) |
void |
tx(BiConsumerWithException<QueryRunner,QueryRunner.Transaction> txQueryRunner)
Provides a
QueryRunner that can be used in a transaction. |
public static QueryRunner create(Connection connection)
try (Connection connection = DriverManager.getConnection(jdbcUrl)) {
QueryRunner queryRunner = QueryRunner.create(connection);
}
connection - the Connection to be used. Must be managed by the callerConnection-based QueryRunnerpublic QueryRunner initializeWith(ConsumerWithException<Connection> initializer)
public <T> T select(String sql, ResultSetHandler<T> resultSetHandler, Object... params) throws Exception
public <T> T insert(String sql, ResultSetHandler<T> resultSetHandler, Object... params) throws Exception
T - the type of instance to returnsql - the INSERT to executeresultSetHandler - transforms the ResultSet, which contains the generated keysparams - values for the SQL placeholdersException - if anything goes wrongpublic int[] batch(String sql, List<List<Object>> params) throws Exception
sql - the SQL to executeparams - values for the SQL placeholders. Eg. Arrays.asList(Arrays.asList(1L), Arrays.asList(2L)) will create a batch of 2 queries, using 1, then 2, as values.Exception - if anything goes wrongpublic <T> T batchInsert(String sql, ResultSetHandler<T> resultSetHandler, List<List<Object>> batchParams) throws Exception
T - the type of instance to returnsql - The INSERT to executeresultSetHandler - transforms the ResultSet, which contains the generated keysbatchParams - values for the SQL placeholders. Eg. Arrays.asList(Arrays.asList(1L), Arrays.asList(2L)) will create a batch of 2 queries, using 1, then 2, as values.Exception - if anything goes wrongpublic void tx(BiConsumerWithException<QueryRunner,QueryRunner.Transaction> txQueryRunner) throws Exception
Provides a QueryRunner that can be used in a transaction.
The underlying Connection is configured in the same way as any other Connection from this QueryRunner, except that
Connection.setAutoCommit(boolean) is set to false before any calls are made and set to true afterwards.
Make sure to call Connection.commit() or Connection.rollback()!
txQueryRunner - Make sure to use this QueryRunner in the transaction blockException - if anything goes wrongCopyright © 2014. All rights reserved.