public class H2Helper extends Object
| Modifier and Type | Class and Description |
|---|---|
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.
|
| Modifier and Type | Method and Description |
|---|---|
Connection |
connect()
Opens and returns a connection to the database as user "sa".
|
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.
|
int |
doUpdate(String sql,
H2Helper.PrepareCallback pc)
Perform database update, doing Connection and PreparedStatement setup and cleanup.
|
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.
|
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 Connection connect() throws SQLException
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
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
conn - Database connection to use.sql - Update SQL.pc - Callback for preparing update. Can be null.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
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.