public class DatabaseWrapper extends Object
SqlParameter
to represent a generic
parameter (name and value).Modifier and Type | Method and Description |
---|---|
void |
close()
If the database is open this will close the JDBC
Connection . |
Statement |
createStatement()
Creates a
Statement using the JDBC Connection . |
void |
destroy()
This will close the JDBC
Connection and then delete the database file. |
void |
dropTable(String tableName)
This will drop the table from the database.
|
long |
executeCountQuery(String countSql)
This will execute the count query and return the result.
|
long |
executeCountQueryWithParameters(ParameterizedSql parameterizedSql)
This will execute the count query with the given parameters and return the result.
|
long |
executeCountQueryWithParameters(String countSql,
SqlParameter... parameters)
This will execute the count query with the given parameters and return the result.
|
int |
executeUpdate(String sql)
The will execute the given sql as an update (insert, update, delete, etc).
|
int |
executeUpdateWithParameters(ParameterizedSql parameterizedSql)
The will execute the given sql as an update with the given parameters (insert, update, delete, etc).
|
int |
executeUpdateWithParameters(String sql,
SqlParameter... parameters)
The will execute the given sql as an update with the given parameters (insert, update, delete, etc).
|
static org.threeten.bp.ZonedDateTime |
fetchLocalTime(ResultSet resultSet,
String columnName)
|
static UUID |
fetchUUID(ResultSet resultSet,
String columnName)
|
void |
fillStatement(PreparedStatement statement,
SqlParameter... parameters)
This is a utility method to help fill
PreparedStatement s (by calling SqlParameter.fillStatement(int, PreparedStatement) . |
List<String> |
getColumnNames(String tableName)
Gets the names of all the columns of a table.
|
String |
getLocation()
Get the file location of the database.
|
int |
getVersion()
Get the version of the database.
|
boolean |
isOpen()
Get if the database is open.
|
DatabaseOpenState |
openOrCreate()
Opens a database for operation.
|
PreparedStatement |
prepareStatement(ParameterizedSql parameterizedSql)
Create a
PreparedStatement for the given sql and fill the parameters. |
PreparedStatement |
prepareStatement(String sql,
SqlParameter... parameters)
Create a
PreparedStatement for the given sql and fill the parameters. |
void |
setVersion(int version)
Set the version of the database.
|
boolean |
tableExists(String tableName)
Check if a table exists in the database.
|
static org.threeten.bp.ZonedDateTime |
toLocalTime(long secondsSinceEpoch)
This will take the passed in time (assumed seconds since Epoch in the UTC time zone) and convert to the local
time zone.
|
static long |
toUTCTimeAsLong(org.threeten.bp.ZonedDateTime dateTime)
Get the time represented as a long (seconds since Epoch) and in the UTC time zone.
|
public static org.threeten.bp.ZonedDateTime fetchLocalTime(ResultSet resultSet, String columnName) throws SQLException
Long
into a ZonedDateTime
from the ResultSet
based on a column
name.
NOTE: See toLocalTime(long)
resultSet
- the result set from the DatabaseWrapper
columnName
- the column name to fetchZonedDateTime
or nullSQLException
- thrown if an error occurs accessing the ResultSet
public static UUID fetchUUID(ResultSet resultSet, String columnName) throws SQLException
String
into a UUID
from the ResultSet
based on a column name.
NOTE: See UUID.fromString(String)
resultSet
- the result set from the DatabaseWrapper
columnName
- the column name to fetchUUID
or nullSQLException
- thrown if an error occurs accessing the ResultSet
public static org.threeten.bp.ZonedDateTime toLocalTime(long secondsSinceEpoch)
secondsSinceEpoch
- the seconds since Epoch in the UTC time zone to convert to local timepublic static long toUTCTimeAsLong(org.threeten.bp.ZonedDateTime dateTime)
dateTime
- the time to convert to a long in UTCpublic void close() throws SQLException
Connection
.SQLException
- thrown if there was an issue closing the Connection
public Statement createStatement() throws SQLException
Statement
using the JDBC Connection
.
NOTE: the database must be open before you call this method.
Statement
SQLException
- thrown if there was an issue creating the Statement
public void destroy() throws IOException, SQLException
Connection
and then delete the database file.
NOTE: the database file will be deleted and cannot be recovered!
IOException
- thrown if there was an issue deleting the database fileSQLException
- thrown if there was an issue closing the Connection
public void dropTable(String tableName) throws SQLException
NOTE: the database must be open before you call this method.
tableName
- the name of the table to dropSQLException
- thrown if there was an issue dropping the tablepublic long executeCountQuery(String countSql) throws SQLException
NOTE: the database must be open before you call this method.
countSql
- the count query to runSQLException
- thrown if there was an issue executing the count querypublic long executeCountQueryWithParameters(ParameterizedSql parameterizedSql) throws SQLException
NOTE: the database must be open before you call this method.
parameterizedSql
- the count query to run and the parameters for the querySQLException
- thrown if there was an issue executing the count querypublic long executeCountQueryWithParameters(String countSql, SqlParameter... parameters) throws SQLException
NOTE: the database must be open before you call this method.
countSql
- the count query to runparameters
- the parameters for the querySQLException
- thrown if there was an issue executing the count querypublic int executeUpdate(String sql) throws SQLException
NOTE: the database must be open before you call this method.
sql
- the update sql to executeSQLException
- thrown if there is an issue executing the updatepublic int executeUpdateWithParameters(ParameterizedSql parameterizedSql) throws SQLException
NOTE: the database must be open before you call this method.
parameterizedSql
- the update sql to execute and the parameters to use in the executionSQLException
- thrown if there is an issue executing the updatepublic int executeUpdateWithParameters(String sql, SqlParameter... parameters) throws SQLException
NOTE: the database must be open before you call this method.
sql
- the update sql to executeparameters
- the parameters to use in the executionSQLException
- thrown if there is an issue executing the updatepublic void fillStatement(PreparedStatement statement, SqlParameter... parameters) throws SQLException
PreparedStatement
s (by calling SqlParameter.fillStatement(int, PreparedStatement)
. This assumes the passed in parameters are in same the order
as they are in the PreparedStatement
.statement
- the statement to fillparameters
- the parameters to fill the statement withSQLException
- thrown if an error occurs filling the statementpublic List<String> getColumnNames(String tableName) throws SQLException
NOTE: the database must be open before you call this method.
tableName
- the table in questionSQLException
- thrown if there was an issue getting the names of the columns in the databasepublic String getLocation()
public int getVersion() throws SQLException
NOTE: the database must be open before you call this method.
SQLException
- thrown if there was an issue querying the version of the databasepublic void setVersion(int version) throws SQLException
NOTE: the database must be open before you call this method.
version
- the version of the databaseSQLException
- thrown if there was an issue setting the version of the databasepublic boolean isOpen()
public DatabaseOpenState openOrCreate() throws SQLException, IOException
location
for database file and hexEncodedAes256Key
as the AES256 HEX
encoded key for the database.
NOTE: the database must be open before you call this method.
DatabaseOpenState.NEW
) or already existed (DatabaseOpenState.EXISTING
)SQLException
- thrown if there was an issue creating or opening the databaseIOException
- thrown if there was an issue with the database locationpublic PreparedStatement prepareStatement(String sql, SqlParameter... parameters) throws SQLException
PreparedStatement
for the given sql and fill the parameters.
NOTE: the database must be open before you call this method.
sql
- the sql to create a PreparedStatement
withparameters
- the parameters to fill the statement withPreparedStatement
SQLException
- thrown if there was an issue creating the PreparedStatement
public PreparedStatement prepareStatement(ParameterizedSql parameterizedSql) throws SQLException
PreparedStatement
for the given sql and fill the parameters.
NOTE: the database must be open before you call this method.
parameterizedSql
- the sql to create a PreparedStatement
with and the parameters to fill the
statement withPreparedStatement
SQLException
- thrown if there was an issue creating the PreparedStatement
public boolean tableExists(String tableName) throws SQLException
NOTE: the database must be open before you call this method.
tableName
- the name of the tableSQLException
- thrown if there was an issue checking if the table existsCopyright © 2021. All rights reserved.