Class FluentJdbc
- java.lang.Object
-
- org.soulwing.jdbc.FluentJdbc
-
- All Implemented Interfaces:
JdbcOperations
public class FluentJdbc extends Object implements JdbcOperations
A thread-safeJdbcOperationsimplementation.This class must be constructed with a
DataSourcethat will be used as necessary to obtain connections to the database.import javax.sql.DataSource; import org.soulwing.jdbc.FluentJdbc; DataSource dataSource = ... // typically injected or retrieved via JNDI FluentJdbc sqlTemplate = new FluentJdbc(dataSource);
A single instance of this class that is constructed with an appropriateDataSourcecan be concurrently shared by an arbitrary number of application components.- Author:
- Carl Harris
-
-
Constructor Summary
Constructors Constructor Description FluentJdbc(Connection connection)Constructs a new instance for use by a single thread.FluentJdbc(DataSource dataSource)Constructs a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description JdbcCallcall(String sql)Creates a call operation.JdbcCallcall(SQLSource source)Creates a call operation.voidexecute(String sql)Executes a single SQL (DDL) statement.voidexecute(SQLSource source)Executes a single SQL (DDL) statement.voidexecuteScript(SQLSource source)Executes the sequence of SQL statements produced by the given source.booleanisAutoCommit()Gets theautoCommitflag state.booleanisIgnoreErrors()Gets theignoreErrorsflag state.JdbcQuery<Void>query()Creates a query operation.<T> JdbcQuery<T>queryForType(Class<T> type)Creates a query operation for objects of a given type.voidsetAutoCommit(boolean autoCommit)Sets theautoCommitflag.voidsetIgnoreErrors(boolean ignoreErrors)Sets theignoreErrorsflag.voidsetLogger(PrintStream stream)Sets a print stream to use for SQL statement logging.voidsetLogger(PrintStream stream, boolean traceEnabled)Sets a print stream to use for SQL statement logging.voidsetLogger(PrintWriter writer)Sets a print writer to use for SQL statement logging.voidsetLogger(PrintWriter writer, boolean traceEnabled)Sets a print writer to use for SQL statement logging.voidsetLogger(JdbcLogger logger)Sets the logger to use for SQL statement logging.JdbcUpdateupdate()Creates an update operation.
-
-
-
Constructor Detail
-
FluentJdbc
public FluentJdbc(DataSource dataSource)
Constructs a new instance.- Parameters:
dataSource- data source that will be used to obtain connections to the database
-
FluentJdbc
public FluentJdbc(Connection connection)
Constructs a new instance for use by a single thread.Use this constructor to create a facade instance that uses the given connection whenever a connection to the database is needed. This allows the facade to be used in conjunction with a framework such as Flyway that wants to explicit manage transaction state by providing a specific connection to each operation that needs one.
Because an instance created via this constructor uses a single database connection, it should not be used by multiple concurrent threads.
- Parameters:
connection- connection that will be used for all JDBC operations invoked using this facade instance
-
-
Method Detail
-
execute
public void execute(String sql)
Executes a single SQL (DDL) statement.- Specified by:
executein interfaceJdbcOperations- Parameters:
sql- the SQL statement to execute
-
execute
public void execute(SQLSource source)
Executes a single SQL (DDL) statement.- Specified by:
executein interfaceJdbcOperations- Parameters:
source- source for the SQL statement to execute
-
executeScript
public void executeScript(SQLSource source)
Executes the sequence of SQL statements produced by the given source.- Specified by:
executeScriptin interfaceJdbcOperations- Parameters:
source- source of the SQL statements to execute
-
query
public JdbcQuery<Void> query()
Creates a query operation.This is a synonym for
JdbcOperations.queryForType(Class)withVoidas the specified type. A query obtained via this method is typically configured to use aResultSetHandlerto process returned rows.- Specified by:
queryin interfaceJdbcOperations- Returns:
- query object that can be configured and executed to retrieve rows from the database
-
queryForType
public <T> JdbcQuery<T> queryForType(Class<T> type)
Creates a query operation for objects of a given type.- Specified by:
queryForTypein interfaceJdbcOperations- Parameters:
type- subject type of the query- Returns:
- a query that can be configured and executed to retreive rows
from the database and return objects of type
T.
-
update
public JdbcUpdate update()
Creates an update operation.- Specified by:
updatein interfaceJdbcOperations- Returns:
- an updater that can be configured and executed to update rows in the database.
-
call
public JdbcCall call(String sql)
Creates a call operation.- Specified by:
callin interfaceJdbcOperations- Parameters:
sql- SQL call statement- Returns:
- call operation
-
call
public JdbcCall call(SQLSource source)
Creates a call operation.- Specified by:
callin interfaceJdbcOperations- Parameters:
source- source for the SQL call statement- Returns:
- call operation;
-
setLogger
public void setLogger(JdbcLogger logger)
Sets the logger to use for SQL statement logging.- Parameters:
logger- the logger to set (may benullto disable logging)
-
setLogger
public void setLogger(PrintWriter writer)
Sets a print writer to use for SQL statement logging.- Parameters:
writer- the logger to set (may benullto disable logging)
-
setLogger
public void setLogger(PrintWriter writer, boolean traceEnabled)
Sets a print writer to use for SQL statement logging.- Parameters:
writer- the logger to set (may benullto disable logging)traceEnabled- flag indicating whether trace level logging should be used
-
setLogger
public void setLogger(PrintStream stream)
Sets a print stream to use for SQL statement logging.- Parameters:
stream- the logger to set (may benullto disable logging)
-
setLogger
public void setLogger(PrintStream stream, boolean traceEnabled)
Sets a print stream to use for SQL statement logging.- Parameters:
stream- the logger to set (may benullto disable logging)traceEnabled- flag indicating whether trace level logging should be used
-
isAutoCommit
public boolean isAutoCommit()
Gets theautoCommitflag state.- Returns:
trueifexecuteScriptwill use auto-commit mode
-
setAutoCommit
public void setAutoCommit(boolean autoCommit)
Sets theautoCommitflag.This state of this flag is checked when
executeScriptis invoked. Iftrue, all statements executed by the script will be auto-committed.In a managed transaction environment (e.g. in a Java EE application) setting this flag to
truemay cause script execution to fail.- Parameters:
autoCommit- the auto-commit flag state to set
-
isIgnoreErrors
public boolean isIgnoreErrors()
Gets theignoreErrorsflag state.- Returns:
trueifexecuteScriptwill ignore errors at the statement level
-
setIgnoreErrors
public void setIgnoreErrors(boolean ignoreErrors)
Sets theignoreErrorsflag.This state of this flag is checked when
executeScriptis invoked. Iftrue, errors thrown by the execution of any given statement will be ignored.In general, ignoring errors is effective only when the auto-commit flag is also set to
true.- Parameters:
ignoreErrors- the flag state to set
-
-