public class H2Helper extends Object
| Modifier and Type | Class and Description |
|---|---|
static interface |
H2Helper.GeneratedKeysCallback<T>
Callback used for processing update generated keys ResultSet.
|
static interface |
H2Helper.PrepareCallback
Callback used for working with PreparedStatement before executing query.
|
static interface |
H2Helper.ResultCallback<T>
Callback used for processing query ResultSet.
|
static interface |
H2Helper.UpgradeCallback
Callback used for creating and upgrading database tables.
|
| Constructor and Description |
|---|
H2Helper(Path datafile)
Create instance of H2DB using given database, and initialize table version table.
|
H2Helper(Path datafile,
Map<String,String> options)
Create instance of H2DB using given database and options, and initialize table version table.
|
H2Helper(Path datafile,
Map<String,String> options,
String user,
String password)
Create instance of H2DB using given database, options and login credentials, and initialize table version table.
|
H2Helper(String connUrl)
Create instance of H2DB using given connection URL, and initialize table version table.
|
| Modifier and Type | Method and Description |
|---|---|
Connection |
connect()
Opens and returns a connection to the database.
|
Connection |
connect(String user,
String password)
Opens and returns a connection to the database as given user.
|
int[] |
doBatchUpdate(Connection conn,
String sql,
H2Helper.PrepareCallback pc)
Perform database batch update transaction, doing PreparedStatement setup and cleanup.
|
int[] |
doBatchUpdate(String sql,
H2Helper.PrepareCallback pc)
Perform database batch update transaction, doing Connection and PreparedStatement setup and cleanup.
|
<T> T |
doQuery(Connection conn,
String sql,
H2Helper.PrepareCallback pc,
H2Helper.ResultCallback<T> rc)
Perform database query, doing PreparedStatement and ResultSet setup and cleanup.
|
<T> T |
doQuery(String sql,
H2Helper.PrepareCallback pc,
H2Helper.ResultCallback<T> rc)
Perform database query, doing Connection, PreparedStatement and ResultSet setup and cleanup.
|
int |
doUpdate(Connection conn,
String sql,
H2Helper.PrepareCallback pc)
Perform database update, doing PreparedStatement setup and cleanup.
|
<T> T |
doUpdate(Connection conn,
String sql,
H2Helper.PrepareCallback pc,
H2Helper.GeneratedKeysCallback<T> gkc)
Perform database update, doing PreparedStatement and ResultSet setup and cleanup.
|
int |
doUpdate(String sql,
H2Helper.PrepareCallback pc)
Perform database update, doing Connection and PreparedStatement setup and cleanup.
|
<T> T |
doUpdate(String sql,
H2Helper.PrepareCallback pc,
H2Helper.GeneratedKeysCallback<T> gkc)
Perform database update, doing Connection, PreparedStatement and ResultSet setup and cleanup.
|
String |
getConnUrl()
Returns the connection url used to connect to the database.
|
void |
initTable(String name,
int version,
H2Helper.UpgradeCallback upgrade)
Checks the version of the given table against the given version, and calls the upgrade callback if necessary.
|
void |
setCredentials(String user,
String password)
Sets login credentials to be used by connect() and other methods which do not have a Connection argument.
|
public H2Helper(String connUrl) throws ClassNotFoundException, SQLException
connUrl - H2 JDBC connection URL.ClassNotFoundException - If error loading database driver.SQLException - If database error occurs.public H2Helper(Path datafile) throws IOException, ClassNotFoundException, SQLException
datafile - Path to database.IOException - If error creating database directory.ClassNotFoundException - If error loading database driver.SQLException - If database error occurs.public H2Helper(Path datafile, Map<String,String> options) throws IOException, ClassNotFoundException, SQLException
datafile - Path to database.options - Map of options to be added to connection URL as ";KEY=VALUE";IOException - If error creating database directory.ClassNotFoundException - If error loading database driver.SQLException - If database error occurs.public H2Helper(Path datafile, Map<String,String> options, String user, String password) throws IOException, ClassNotFoundException, SQLException
datafile - Path to database.options - Map of options to be added to connection URL as ";KEY=VALUE";user - Username to use for connections.password - Password to use for connections.IOException - If error creating database directory.ClassNotFoundException - If error loading database driver.SQLException - If database error occurs.public void setCredentials(String user, String password)
If these have not been set, or user is set to null here, the H2 default ("sa"/"") is used. For any usage where security matters that should have been changed, and you will need to provide credentials here.
user - Username to use for connections.password - Password to use for connections.public String getConnUrl()
public Connection connect() throws SQLException
Uses credentials set by setCredentials(), or the H2 defaults.
SQLException - If database error occurs.public Connection connect(String user, String password) throws SQLException
user - Username for connection.password - Password for connection.SQLException - If database error occurs.public void initTable(String name, int version, H2Helper.UpgradeCallback upgrade) throws SQLException
name - Name of table to check.version - Desired table version.upgrade - Callback to use if the current version is less than desired version.SQLException - If database error occurs.public <T> T doQuery(String sql, H2Helper.PrepareCallback pc, H2Helper.ResultCallback<T> rc) throws SQLException
T - Type of data object to return.sql - Query SQL.pc - Callback for preparing query. Can be null.rc - Callback for processing result set.SQLException - If database error occurs.public <T> T doQuery(Connection conn, String sql, H2Helper.PrepareCallback pc, H2Helper.ResultCallback<T> rc) throws SQLException
Synchronizes on conn, to avoid concurrency problems if the caller is using multiple threads.
T - Type of data object to return.conn - Database connection to use.sql - Query SQL.pc - Callback for preparing query. Can be null.rc - Callback for processing result set.SQLException - If database error occurs.public int doUpdate(String sql, H2Helper.PrepareCallback pc) throws SQLException
sql - Update SQL.pc - Callback for preparing update. Can be null.SQLException - If database error occurs.public int doUpdate(Connection conn, String sql, H2Helper.PrepareCallback pc) throws SQLException
Synchronizes on conn, to avoid concurrency problems if the caller is using multiple threads.
conn - Database connection to use.sql - Update SQL.pc - Callback for preparing update. Can be null.SQLException - If database error occurs.public <T> T doUpdate(String sql, H2Helper.PrepareCallback pc, H2Helper.GeneratedKeysCallback<T> gkc) throws SQLException
T - Type of data object to return.sql - Update SQL.pc - Callback for preparing update. Can be null.gkc - Callback for processing generated keys result set.SQLException - If database error occurs.public <T> T doUpdate(Connection conn, String sql, H2Helper.PrepareCallback pc, H2Helper.GeneratedKeysCallback<T> gkc) throws SQLException
Synchronizes on conn, to avoid concurrency problems if the caller is using multiple threads.
T - Type of data object to return.conn - Database connection to use.sql - Update SQL.pc - Callback for preparing update. Can be null.gkc - Callback for processing generated keys result set.SQLException - If database error occurs.public int[] doBatchUpdate(String sql, H2Helper.PrepareCallback pc) throws SQLException
sql - Batch update SQL.pc - Callback for preparing batch update. Can be null.SQLException - If database error occurs.public int[] doBatchUpdate(Connection conn, String sql, H2Helper.PrepareCallback pc) throws SQLException
Synchronizes on conn, to avoid concurrency problems if the caller is using multiple threads.
conn - Database connection to use.sql - Batch update SQL.pc - Callback for preparing batch update. Can be null.SQLException - If database error occurs.Copyright © 2018. All rights reserved.