Class JdbcUtils

java.lang.Object
org.axonframework.common.jdbc.JdbcUtils

public class JdbcUtils extends Object
Utility class for safely executing Jdbc queries.
Since:
2.2
Author:
Kristian Rosenvold, Allard Buijze, Rene de Waele
  • 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 the 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.
      Type Parameters:
      R - the type result of the query
      Parameters:
      connection - connection to the underlying database that should be used for the query
      sqlFunction - the function that returns a PreparedStatement to execute the query against
      sqlResultConverter - converts the result set to a value of type R
      errorHandler - 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 the 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.
      Type Parameters:
      R - the type result of the query
      Parameters:
      connection - connection to the underlying database that should be used for the query
      sqlFunction - the function that returns a PreparedStatement to execute the query against
      sqlResultConverter - converts the result set to a value of type R
      errorHandler - handles errors as result of executing the query or converting the result set
      closeConnection - whether provided connection should 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 given updateFunction. Any errors will be handled by the given errorHandler.
      Parameters:
      connection - connection to the underlying database that should be used for the update
      updateFunction - the function that produce the update statement
      errorHandler - 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 given sqlFunctions. Any errors will be handled by the given errorHandler.
      Parameters:
      connection - connection to the underlying database that should be used for the update
      errorHandler - handles errors as result of executing the update
      sqlFunctions - 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 given sqlFunction. Any errors will be handled by the given errorHandler.
      Parameters:
      connection - connection to the underlying database that should be used for the update
      sqlFunction - the function that produces the batch update statement
      errorHandler - 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 type 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.
      Type Parameters:
      R - the type of result produced by the singleResultConverter
      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

      public static void closeQuietly(ResultSet resultSet)
      Close the given resultSet, if possible. All exceptions are discarded.
      Parameters:
      resultSet - The resource to close. May be null.
    • closeQuietly

      public static void closeQuietly(Statement statement)
      Close the given statement, if possible. All exceptions are discarded.
      Parameters:
      statement - The resource to close. May be null.
    • closeQuietly

      public static void closeQuietly(Connection connection)
      Close the given connection, if possible. All exceptions are discarded.
      Parameters:
      connection - The resource to close. May be null.
    • nextAndExtract

      public static <T> T nextAndExtract(ResultSet resultSet, int column, Class<T> columnType) throws SQLException, NullPointerException
      Moves the resultSet cursor 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.

      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 null if no data was present.
      Throws:
      SQLException - If an error occurs while reading the object.
      NullPointerException - If the resultSet or columnType are null.
      See Also:
    • nextAndExtract

      public static <T> T nextAndExtract(ResultSet resultSet, int column, Class<T> columnType, T defaultValue) throws SQLException, NullPointerException
      Moves the resultSet cursor 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 the given defaultValue. This method reads the object at the given column (base 1), which may be defaulted to the defaultValue as well.

      Please use the method extract(ResultSet, int, Class, Object) 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.

      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 the resultSet.
      Returns:
      The next value in the specified column or the defaultValue if no data was present.
      Throws:
      SQLException - If an error occurs while reading the object.
      NullPointerException - If the resultSet or columnType are null.
      See Also:
    • extract

      public static <T> T extract(ResultSet resultSet, int column, Class<T> columnType) throws SQLException, NullPointerException
      Returns the object read from the 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.

      Parameters:
      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).
      Returns:
      The object read from the resultSet, which object can be null, at the given column (base 1).
      Throws:
      SQLException - If an error occurs while reading the object.
      NullPointerException - If the resultSet or columnType are null.
    • extract

      public static <T> T extract(ResultSet resultSet, int column, Class<T> columnType, T defaultValue) throws SQLException, NullPointerException
      Returns the object read from the 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.

      Parameters:
      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)
      Returns:
      the object read from the resultSet, which object can be null, at the given column (base 1).
      Throws:
      SQLException - if an error occurs while reading the object
      NullPointerException - if the resultSet or columnType are null