java.lang.Object
org.itsallcode.jdbc.SimpleConnection
- All Implemented Interfaces:
AutoCloseable
,DbOperations
A simplified version of a JDBC
Connection
. Create new connections
with
-
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Close the underlyingConnection
.void
executeScript
(String sqlScript) Execute all commands in a SQL script, separated with;
.int
executeUpdate
(String sql) Execute a single SQL statement.int
executeUpdate
(String sql, PreparedStatementSetter preparedStatementSetter) Execute a single SQL statement as a prepared statement with placeholders.Get database metadata.Get the original wrapped connection.Create a prepared statement batch builder for inserting or updating rows by directly setting values of aPreparedStatement
.preparedStatementBatch
(Class<T> rowType) Execute a SQL query and return aresult set
with genericRow
s.<T> SimpleResultSet<T>
query
(String sql, PreparedStatementSetter preparedStatementSetter, RowMapper<T> rowMapper) Execute a SQL query, set parameters and return aresult set
with rows converted to a custom typeDbOperations
.Start a newTransaction
by disabling auto commit if necessary.Create a batch statement builder for executing multiple statements in a batch.static SimpleConnection
wrap
(Connection connection, DbDialect dialect) Wrap an existingConnection
with aSimpleConnection
.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.itsallcode.jdbc.DbOperations
executeUpdate, query, query
-
Method Details
-
wrap
Wrap an existingConnection
with aSimpleConnection
.Note: Calling
close()
will close the underlying connection.- Parameters:
connection
- existing connectiondialect
- database dialect- Returns:
- wrapped connection
-
startTransaction
Start a newTransaction
by disabling auto commit if necessary.Important: The transaction must be committed or rolled back before the connection can be used again.
- Returns:
- new transaction
-
executeUpdate
Description copied from interface:DbOperations
Execute a single SQL statement.- Specified by:
executeUpdate
in interfaceDbOperations
- Parameters:
sql
- SQL statement- Returns:
- either the row count for SQL Data Manipulation Language (DML) statements or 0 for SQL statements that return nothing
-
executeScript
Description copied from interface:DbOperations
Execute all commands in a SQL script, separated with;
.- Specified by:
executeScript
in interfaceDbOperations
- Parameters:
sqlScript
- script to execute.
-
executeUpdate
Description copied from interface:DbOperations
Execute a single SQL statement as a prepared statement with placeholders.- Specified by:
executeUpdate
in interfaceDbOperations
- Parameters:
sql
- SQL statementpreparedStatementSetter
- prepared statement setter- Returns:
- either the row count for SQL Data Manipulation Language (DML) statements or 0 for SQL statements that return nothing
-
query
Description copied from interface:DbOperations
Execute a SQL query and return aresult set
with genericRow
s.- Specified by:
query
in interfaceDbOperations
- Parameters:
sql
- SQL query- Returns:
- result set
-
query
public <T> SimpleResultSet<T> query(String sql, PreparedStatementSetter preparedStatementSetter, RowMapper<T> rowMapper) Description copied from interface:DbOperations
Execute a SQL query, set parameters and return aresult set
with rows converted to a custom typeDbOperations
.- Specified by:
query
in interfaceDbOperations
- Type Parameters:
T
- generic row type- Parameters:
sql
- SQL querypreparedStatementSetter
- the prepared statement setterrowMapper
- row mapper- Returns:
- the result set
-
statementBatch
Description copied from interface:DbOperations
Create a batch statement builder for executing multiple statements in a batch.- Specified by:
statementBatch
in interfaceDbOperations
- Returns:
- batch statement builder
-
preparedStatementBatch
Description copied from interface:DbOperations
Create a prepared statement batch builder for inserting or updating rows by directly setting values of aPreparedStatement
.If you want to insert rows from an
Iterator
or aStream
, useDbOperations.preparedStatementBatch(Class)
.- Specified by:
preparedStatementBatch
in interfaceDbOperations
- Returns:
- batch insert builder
-
preparedStatementBatch
Description copied from interface:DbOperations
Create a row-based prepared statement batch builder for inserting or updating rows from anIterator
or aStream
.If you want to insert rows by directly setting values of a
PreparedStatement
, useDbOperations.preparedStatementBatch()
.- Specified by:
preparedStatementBatch
in interfaceDbOperations
- Type Parameters:
T
- row type- Parameters:
rowType
- row type- Returns:
- row-based batch insert builder
-
getMetaData
Get database metadata.- Returns:
- metadata
-
getOriginalConnection
Description copied from interface:DbOperations
Get the original wrapped connection.Use this in case of missing features in
DbOperations
.- Specified by:
getOriginalConnection
in interfaceDbOperations
- Returns:
- original wrapped connection
-
close
Close the underlyingConnection
.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceDbOperations
- See Also:
-