Class JdbcUtils
- Since:
- 2.2
- Author:
- Kristian Rosenvold, Allard Buijze, Rene de Waele
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceDescribes a function that creates a newPreparedStatementready to be executed.static interfaceDescribes a function that converts aResultSetinto a result of typeR. -
Method Summary
Modifier and TypeMethodDescriptionstatic voidcloseQuietly(Connection connection) Close the givenconnection, if possible.static voidcloseQuietly(ResultSet resultSet) Close the givenresultSet, if possible.static voidcloseQuietly(Statement statement) Close the givenstatement, if possible.static int[]executeBatch(Connection connection, JdbcUtils.SqlFunction sqlFunction, Consumer<SQLException> errorHandler) Execute the a batch update or insert statement produced by the givensqlFunction.static <R> RexecuteQuery(Connection connection, JdbcUtils.SqlFunction sqlFunction, JdbcUtils.SqlResultConverter<R> sqlResultConverter, Function<SQLException, RuntimeException> errorHandler) Execute the query given by thesqlFunction.static <R> RexecuteQuery(Connection connection, JdbcUtils.SqlFunction sqlFunction, JdbcUtils.SqlResultConverter<R> sqlResultConverter, Function<SQLException, RuntimeException> errorHandler, boolean closeConnection) Execute the query given by thesqlFunction.static intexecuteUpdate(Connection connection, JdbcUtils.SqlFunction updateFunction, Function<SQLException, RuntimeException> errorHandler) Execute the update statement produced by the givenupdateFunction.static int[]executeUpdates(Connection connection, Consumer<SQLException> errorHandler, JdbcUtils.SqlFunction... sqlFunctions) Execute the update statements produced by the givensqlFunctions.static <T> TReturns the object read from theresultSet, which object can benull, at the given column (base 1).static <T> TReturns the object read from theresultSet, which object can benull, 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 typeRfrom a converter that produces a single result.static <T> TnextAndExtract(ResultSet resultSet, int column, Class<T> columnType) Moves theresultSetcursor forward and then read the object at the given column (base 1).static <T> TnextAndExtract(ResultSet resultSet, int column, Class<T> columnType, T defaultValue) Moves theresultSetcursor forward and then read the object at the given column (base 1).
-
Method Details
-
executeQuery
public static <R> R executeQuery(Connection connection, JdbcUtils.SqlFunction sqlFunction, JdbcUtils.SqlResultConverter<R> sqlResultConverter, Function<SQLException, RuntimeException> errorHandler) Execute the query given by thesqlFunction. TheResultSetreturned when the query is executed will be converted using the givensqlResultConverter. Any errors will be handled by the givenerrorHandler.- Type Parameters:
R- the type result of the query- Parameters:
connection- connection to the underlying database that should be used for the querysqlFunction- the function that returns aPreparedStatementto 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 set- Returns:
- the query result the result of the query
-
executeQuery
public static <R> R executeQuery(Connection connection, JdbcUtils.SqlFunction sqlFunction, JdbcUtils.SqlResultConverter<R> sqlResultConverter, Function<SQLException, RuntimeException> errorHandler, boolean closeConnection) Execute the query given by thesqlFunction. TheResultSetreturned when the query is executed will be converted using the givensqlResultConverter. Any errors will be handled by the givenerrorHandler.- Type Parameters:
R- the type result of the query- Parameters:
connection- connection to the underlying database that should be used for the querysqlFunction- the function that returns aPreparedStatementto 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 setcloseConnection- whether providedconnectionshould be closed or not- Returns:
- the query result the result of the query
-
executeUpdate
public static int executeUpdate(Connection connection, JdbcUtils.SqlFunction updateFunction, Function<SQLException, RuntimeException> errorHandler) Execute the update statement produced by the givenupdateFunction. Any errors will be handled by the givenerrorHandler.- Parameters:
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 update- Returns:
- the update count resulting from the given
updateFunction
-
executeUpdates
public static int[] executeUpdates(Connection connection, Consumer<SQLException> errorHandler, JdbcUtils.SqlFunction... sqlFunctions) Execute the update statements produced by the givensqlFunctions. Any errors will be handled by the givenerrorHandler.- Parameters:
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 statements- Returns:
- an array of update counts containing one element for each sql function
-
executeBatch
public static int[] executeBatch(Connection connection, JdbcUtils.SqlFunction sqlFunction, Consumer<SQLException> errorHandler) Execute the a batch update or insert statement produced by the givensqlFunction. Any errors will be handled by the givenerrorHandler.- Parameters:
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 update- Returns:
- an array of update counts containing one element for each sql function
-
listResults
public static <R> JdbcUtils.SqlResultConverter<List<R>> listResults(JdbcUtils.SqlResultConverter<R> singleResultConverter) Create a converter that produces a List of results of typeRfrom 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.- Type Parameters:
R- the type of result produced by thesingleResultConverter- Parameters:
singleResultConverter- the converter that can convert a single result from the current position of the resultSet- Returns:
- converter that produces a list of results
-
closeQuietly
Close the givenresultSet, if possible. All exceptions are discarded.- Parameters:
resultSet- The resource to close. May benull.
-
closeQuietly
Close the givenstatement, if possible. All exceptions are discarded.- Parameters:
statement- The resource to close. May benull.
-
closeQuietly
Close the givenconnection, if possible. All exceptions are discarded.- Parameters:
connection- The resource to close. May benull.
-
nextAndExtract
public static <T> T nextAndExtract(ResultSet resultSet, int column, Class<T> columnType) throws SQLException, NullPointerException Moves theresultSetcursor forward and then read the object at the given column (base 1). Please note that this method changes theresultSetcursor position. If theresultSetreaches the end, this method returnsnull. This method reads the object at the given column (base 1), which object can benullas well.Please use the method
extract(ResultSet, int, Class)if you do not need to move theresultSetcursor.This method makes use of the
ResultSet.wasNull()method to verify whether the object that was read wasnullor not. There are cases where the database driver (such as MySQL) returns the default primitive value instead ofnull. This method avoids this problem.- Parameters:
resultSet- The ResultSet to extract data from.column- The index of the column containing to read.columnType- The expected type of data in the column.- Returns:
- The next value in the specified column or
nullif no data was present. - Throws:
SQLException- If an error occurs while reading the object.NullPointerException- If theresultSetorcolumnTypearenull.- See Also:
-
nextAndExtract
public static <T> T nextAndExtract(ResultSet resultSet, int column, Class<T> columnType, T defaultValue) throws SQLException, NullPointerException Moves theresultSetcursor forward and then read the object at the given column (base 1). Please note that this method changes theresultSetcursor position. If theresultSetreaches the end, this method returns the givendefaultValue. This method reads the object at the given column (base 1), which may be defaulted to thedefaultValueas well.Please use the method
extract(ResultSet, int, Class, Object)if you do not need to move theresultSetcursor.This method makes use of the
ResultSet.wasNull()method to verify whether the object that was read wasnullor not. There are cases where the database driver (such as MySQL) returns the default primitive value instead ofnull. This method avoids this problem.- Parameters:
resultSet- The ResultSet to extract data from.column- The index of the column containing to read.columnType- The expected type of data in the column.defaultValue- The default value to return if no data was present theresultSet.- Returns:
- The next value in the specified column or the
defaultValueif no data was present. - Throws:
SQLException- If an error occurs while reading the object.NullPointerException- If theresultSetorcolumnTypearenull.- See Also:
-
extract
public static <T> T extract(ResultSet resultSet, int column, Class<T> columnType) throws SQLException, NullPointerException Returns the object read from theresultSet, which object can benull, at the given column (base 1).This method makes use of the
ResultSet.wasNull()method to verify whether the object that was read wasnullor not. There are cases where the database driver (such as MySQL) returns the default primitive value instead ofnull. This method avoids this problem.- Parameters:
resultSet- The result set from where the object is read (which cannot benull).column- The column index (which starts from 1).columnType- The object type (which cannot benull).- Returns:
- The object read from the
resultSet, which object can benull, at the given column (base 1). - Throws:
SQLException- If an error occurs while reading the object.NullPointerException- If theresultSetorcolumnTypearenull.
-
extract
public static <T> T extract(ResultSet resultSet, int column, Class<T> columnType, T defaultValue) throws SQLException, NullPointerException Returns the object read from theresultSet, which object can benull, at the given column (base 1).This method makes use of the
ResultSet.wasNull()method to verify whether the object that was read wasnullor not. There are cases where the database driver (such as MySQL) returns the default primitive value instead ofnull. This method avoids this problem.- Parameters:
resultSet- the result set from where the object is read (which cannot benull)column- the column index (which starts from 1)columnType- the object type (which cannot benull)- Returns:
- the object read from the
resultSet, which object can benull, at the given column (base 1). - Throws:
SQLException- if an error occurs while reading the objectNullPointerException- if theresultSetorcolumnTypearenull
-