Package africa.absa.inception.core.util
Class JDBCUtil
- java.lang.Object
-
- africa.absa.inception.core.util.JDBCUtil
-
public class JDBCUtil extends Object
The JDBCUtil class provides JDBCUtil utility functions.- Author:
- Marcus Portmann
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidclose(Connection connection)Close the connection.static voidclose(ResultSet rs)Close the result set.static voidclose(Statement statement)Close the statement.static intexecuteStatement(Connection connection, String sql)Execute the SQL statement using the database connection.static intexecuteStatements(Connection connection, String resourcePath)Execute the SQL statements in the file with the specified resource path using the database connection.static StringgetSchemaSeparator(DataSource dataSource)Retrieve the schema separator for the database associated with the specified data source.static booleanisInMemoryH2Database(DataSource dataSource)Is the database associated with the specified data source an in-memory H2 database.static List<String>loadSQL(URL url)Load the SQL statements from the specified URL.static byte[]readBlob(ResultSet rs, int index)Read the blob associated with the column with the specified index from the specified result set.static voidrollback(Connection connection)Rollback the current transaction on the specified connection.static booleanschemaExists(Connection connection, String catalog, String schema)Checks whether the schema with the specified name exists for the database referenced by the connection.static booleanschemaExists(DataSource dataSource, String catalog, String schema)Checks whether the schema with the specified name exists for the database referenced by the data source.static voidshutdownHsqlDatabase(Connection connection)Close and release all connections that are currently stored in the connection pool associated with the data source.static booleantableExists(Connection connection, String catalog, String schema, String table)Checks whether the table with the specified name exists under the catalog and schema for the database referenced by the data source.static booleantableExists(DataSource dataSource, String catalog, String schema, String table)Checks whether the table with the specified name exists under the catalog and schema for the database referenced by the data source.
-
-
-
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 usesql- 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 useresourcePath- 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 setindex- 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 usecatalog- the catalog name or null if a catalog should not be usedschema- 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 usecatalog- the catalog name or null if a catalog should not be usedschema- 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 usecatalog- the catalog name or null if a catalog should not be usedschema- the schema name or null if a schema should not be usedtable- 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 usecatalog- the catalog name or null if a catalog should not be usedschema- the schema name or null if a schema should not be usedtable- the name of the table- Returns:
- true if the table exists or false otherwise
- Throws:
SQLException- if the check for the existing table failed
-
-