public class ConnectionWrapper
extends java.lang.Object
Example usage 1: Retrieving the user name for the Connection
final Database db = ... try { final ConnectionWrapper wrapper = new ConnectionWrapper( db, "Query username from connection metadata" ); // description should be translated final String userName = wrapper.call( wrapper.new SQLCallableExample usage 2: Setting the autocommit flag on the connection() { public String call() throws SQLException { return getConnection().getMetaData().getUserName(); } } ); } catch( SQLException sqe ) { // process the exception }
final Database db = ... try { final ConnectionWrapper wrapper = new ConnectionWrapper( db, "Set the auto commit flag on the Connection" ); // description should be translated final String userName = wrapper.run( wrapper.new SQLRunnable() { public void run() throws SQLException { getConnection().setAutoCommit( true ); } } ); } catch( SQLException sqe ) { // process the exception }
Modifier and Type | Class and Description |
---|---|
class |
ConnectionWrapper.SQLCallable<T>
Callable class that can throw a SQLException from its call method.
|
class |
ConnectionWrapper.SQLRunnable
Runnable class that can throw a SQLException from its run method.
|
Constructor and Description |
---|
ConnectionWrapper(Database db,
java.lang.String desc)
Creates a ConnectionWrapper using the given Database.
|
Modifier and Type | Method and Description |
---|---|
<T> T |
call(ConnectionWrapper.SQLCallable<T> c)
Calls the given SQLCallable.
|
void |
run(ConnectionWrapper.SQLRunnable r)
Runs the given SQLRunnable.
|
public ConnectionWrapper(Database db, java.lang.String desc)
db
- the database to use the Connection fromdesc
- a translatable description of the nature of the process being
executed. This will be wrapped into any DBException thrown if the process
fails to indicate to the user what failed.public void run(ConnectionWrapper.SQLRunnable r) throws DBException
r
- the runnable to run as a unit of work on the ConnectionDBException
- if an error with the database occurspublic <T> T call(ConnectionWrapper.SQLCallable<T> c) throws DBException
c
- the callable to run as a unit of work on the ConnectionDBException
- if an error with the database occurs