public class JdbcUtils extends Object
Modifier and Type | Class and Description |
---|---|
static interface |
JdbcUtils.SqlFunction
Describes a function that creates a new
PreparedStatement ready to be executed. |
static interface |
JdbcUtils.SqlResultConverter<R>
Describes a function that converts a
ResultSet into a result of type R . |
Modifier and Type | Method and Description |
---|---|
static void |
closeQuietly(Connection connection)
Close the given
connection , if possible. |
static void |
closeQuietly(ResultSet resultSet)
Close the given
resultSet , if possible. |
static void |
closeQuietly(Statement statement)
Close the given
statement , if possible. |
static int[] |
executeBatch(Connection connection,
JdbcUtils.SqlFunction sqlFunction,
Consumer<SQLException> errorHandler)
Execute the a batch update or insert statement produced by the given
sqlFunction . |
static <R> R |
executeQuery(Connection connection,
JdbcUtils.SqlFunction sqlFunction,
JdbcUtils.SqlResultConverter<R> sqlResultConverter,
Function<SQLException,RuntimeException> errorHandler)
Execute the query given by the
sqlFunction . |
static int |
executeUpdate(Connection connection,
JdbcUtils.SqlFunction updateFunction,
Function<SQLException,RuntimeException> errorHandler)
Execute the update statement produced by the given
updateFunction . |
static int[] |
executeUpdates(Connection connection,
Consumer<SQLException> errorHandler,
JdbcUtils.SqlFunction... sqlFunctions)
Execute the update statements produced by the given
sqlFunctions . |
static <T> T |
extract(ResultSet resultSet,
int column,
Class<T> columnType)
Returns the object read from the
resultSet , which object can be
null , at the given column (base 1). |
static <R> JdbcUtils.SqlResultConverter<List<R>> |
listResults(JdbcUtils.SqlResultConverter<R> singleResultConverter)
Create a converter that produces a List of results of type
R from a converter that produces a single
result. |
static <T> T |
nextAndExtract(ResultSet resultSet,
int column,
Class<T> columnType)
Moves the
resultSet courser forward and then read the object at the
given column (base 1). |
public static <R> R executeQuery(Connection connection, JdbcUtils.SqlFunction sqlFunction, JdbcUtils.SqlResultConverter<R> sqlResultConverter, Function<SQLException,RuntimeException> errorHandler)
sqlFunction
. The ResultSet
returned when the query is executed
will be converted using the given sqlResultConverter
. Any errors will be handled by the given errorHandler
.R
- the result of the query after conversionconnection
- connection to the underlying database that should be used for the querysqlFunction
- the function that returns a PreparedStatement
to execute the query againstsqlResultConverter
- converts the result set to a value of type RerrorHandler
- handles errors as result of executing the query or converting the result setpublic static int executeUpdate(Connection connection, JdbcUtils.SqlFunction updateFunction, Function<SQLException,RuntimeException> errorHandler)
updateFunction
. Any errors will be handled by the
given errorHandler
.connection
- connection to the underlying database that should be used for the updateupdateFunction
- the function that produce the update statementerrorHandler
- handles errors as result of executing the updateupdateFunction
public static int[] executeUpdates(Connection connection, Consumer<SQLException> errorHandler, JdbcUtils.SqlFunction... sqlFunctions)
sqlFunctions
. Any errors will be handled by the given
errorHandler
.connection
- connection to the underlying database that should be used for the updateerrorHandler
- handles errors as result of executing the updatesqlFunctions
- the functions that produce the update statementspublic static int[] executeBatch(Connection connection, JdbcUtils.SqlFunction sqlFunction, Consumer<SQLException> errorHandler)
sqlFunction
. Any errors will be
handled by the given errorHandler
.connection
- connection to the underlying database that should be used for the updatesqlFunction
- the function that produces the batch update statementerrorHandler
- handles errors as result of executing the updatepublic static <R> JdbcUtils.SqlResultConverter<List<R>> listResults(JdbcUtils.SqlResultConverter<R> singleResultConverter)
R
from a converter that produces a single
result. The returned converter iterates over the resultSet until all results have been converted and added to
the list.R
- the type of result produced by the singleResultConverter
singleResultConverter
- the converter that can convert a single result from the current position of the
resultSetpublic static void closeQuietly(ResultSet resultSet)
resultSet
, if possible. All exceptions are discarded.resultSet
- The resource to close. May be null
.public static void closeQuietly(Statement statement)
statement
, if possible. All exceptions are discarded.statement
- The resource to close. May be null
.public static void closeQuietly(Connection connection)
connection
, if possible. All exceptions are discarded.connection
- The resource to close. May be null
.public static <T> T nextAndExtract(ResultSet resultSet, int column, Class<T> columnType) throws SQLException, NullPointerException
resultSet
courser forward and then read the object at the
given column (base 1). Please note that this method changes the
resultSet
cursor position. If the resultSet
reaches the end,
this method returns null
. This method reads the object at the given
column (base 1), which object can be null
as well.
Please use the method extract(ResultSet, int, Class)
if you do not
need to move the resultSet
cursor.
This method makes use of the ResultSet.wasNull()
method to verify
whether the object that was read was null
or not. There are cases
where the database driver (such as MySQL) returns the default primitive value
instead of null
. This method avoids this problem.
resultSet
- The ResultSet to extract data fromcolumn
- The index of the column containing to readcolumnType
- The expected type of data in the columnnull
if no data was presentSQLException
- if an error occurs while reading the objectNullPointerException
- if the resultSet
or columnType
are null
extract(ResultSet, int, Class)
public static <T> T extract(ResultSet resultSet, int column, Class<T> columnType) throws SQLException, NullPointerException
resultSet
, which object can be
null
, at the given column (base 1).
This method makes use of the ResultSet.wasNull()
method to verify
whether the object that was read was null
or not. There are cases
where the database driver (such as MySQL) returns the default primitive value
instead of null
. This method avoids this problem.
resultSet
- the result set from where the object is read (which cannot be
null
)column
- the column index (which starts from 1)columnType
- the object type (which cannot be null
)resultSet
, which object can be
null
, at the given column (base 1).SQLException
- if an error occurs while reading the objectNullPointerException
- if the resultSet
or columnType
are null
Copyright © 2010–2020. All rights reserved.