Class JDBCUtil


  • public class JDBCUtil
    extends Object
    The JDBCUtil class provides JDBCUtil utility functions.
    Author:
    Marcus Portmann
    • Method Detail

      • close

        public static void close​(Connection connection)
        Close the connection.
        Parameters:
        connection - the connection to close
      • close

        public static void close​(ResultSet rs)
        Close the result set.
        Parameters:
        rs - the result set to close
      • close

        public static void close​(Statement statement)
        Close the statement.
        Parameters:
        statement - the statement to close
      • executeStatement

        public static int executeStatement​(Connection connection,
                                           String sql)
                                    throws SQLException
        Execute the SQL statement using the database connection.
        Parameters:
        connection - the database connection to use
        sql - the SQL statement to execute
        Returns:
        the row count
        Throws:
        SQLException - if the statement execution failed
      • executeStatements

        public static int executeStatements​(Connection connection,
                                            String resourcePath)
                                     throws SQLException
        Execute the SQL statements in the file with the specified resource path using the database connection.
        Parameters:
        connection - the database connection to use
        resourcePath - the resource path to the file containing the SQL statements
        Returns:
        the number of SQL statements successfully executed
        Throws:
        SQLException - if the statement execution failed
      • getSchemaSeparator

        public static String getSchemaSeparator​(DataSource dataSource)
                                         throws SQLException
        Retrieve the schema separator for the database associated with the specified data source.
        Parameters:
        dataSource - the data source
        Returns:
        the schema separator for the database associated with the specified data source
        Throws:
        SQLException - if the schema separator could not be retrieved
      • isInMemoryH2Database

        public static boolean isInMemoryH2Database​(DataSource dataSource)
                                            throws SQLException
        Is the database associated with the specified data source an in-memory H2 database.
        Parameters:
        dataSource - the data source
        Returns:
        true if the database associated with the specified data source is an in-memory H2 database or false otherwise
        Throws:
        SQLException - if the check for an in-memory H2 database failed
      • loadSQL

        public static List<String> loadSQL​(URL url)
                                    throws IOException
        Load the SQL statements from the specified URL.
        Parameters:
        url - the URL
        Returns:
        the SQL statements loaded from the specified URL
        Throws:
        IOException - if the SQL statements could not be loaded from the specified URL
      • readBlob

        public static byte[] readBlob​(ResultSet rs,
                                      int index)
                               throws SQLException
        Read the blob associated with the column with the specified index from the specified result set.
        Parameters:
        rs - the result set
        index - the index of the column containing the blob
        Returns:
        the binary data for the BLOB
        Throws:
        SQLException - if the blob could not be read
      • rollback

        public static void rollback​(Connection connection)
        Rollback the current transaction on the specified connection.
        Parameters:
        connection - the connection to rollback the transaction on
      • schemaExists

        public static boolean schemaExists​(Connection connection,
                                           String catalog,
                                           String schema)
                                    throws SQLException
        Checks whether the schema with the specified name exists for the database referenced by the connection.
        Parameters:
        connection - the database connection to use
        catalog - the catalog name or null if a catalog should not be used
        schema - the schema name
        Returns:
        true if the schema exists or false otherwise
        Throws:
        SQLException - if the schema check failed
      • schemaExists

        public static boolean schemaExists​(DataSource dataSource,
                                           String catalog,
                                           String schema)
                                    throws SQLException
        Checks whether the schema with the specified name exists for the database referenced by the data source.
        Parameters:
        dataSource - the data source to use
        catalog - the catalog name or null if a catalog should not be used
        schema - the schema name
        Returns:
        true if the schema exists or false otherwise
        Throws:
        SQLException - if the check for the existing schema failed
      • shutdownHsqlDatabase

        public static void shutdownHsqlDatabase​(Connection connection)
                                         throws SQLException
        Close and release all connections that are currently stored in the connection pool associated with the data source.

        The HSQLDB database associated with the data source will also be shutdown.

        Parameters:
        connection - the HSQLDB database connection
        Throws:
        SQLException - if the HSQLDB database could not be shutdown
      • tableExists

        public static boolean tableExists​(Connection connection,
                                          String catalog,
                                          String schema,
                                          String table)
                                   throws SQLException
        Checks whether the table with the specified name exists under the catalog and schema for the database referenced by the data source.
        Parameters:
        connection - the database connection to use
        catalog - the catalog name or null if a catalog should not be used
        schema - the schema name or null if a schema should not be used
        table - the name of the table
        Returns:
        true if the table exists or false otherwise
        Throws:
        SQLException - if the check for the existing table failed
      • tableExists

        public static boolean tableExists​(DataSource dataSource,
                                          String catalog,
                                          String schema,
                                          String table)
                                   throws SQLException
        Checks whether the table with the specified name exists under the catalog and schema for the database referenced by the data source.
        Parameters:
        dataSource - the data source to use
        catalog - the catalog name or null if a catalog should not be used
        schema - the schema name or null if a schema should not be used
        table - the name of the table
        Returns:
        true if the table exists or false otherwise
        Throws:
        SQLException - if the check for the existing table failed