|
[Deprecated API] | ||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | CURRENT API | |||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||
java.lang.Object | +--com.interface21.jdbc.core.JdbcTemplate
This is the central class in this package. It simplifies the use of JDBC and helps to avoid common errors. It executes core JDBC workflow, leaving application code to provide SQL and extract results. This class executes SQL queries or updates, initating iteration over ResultSets and catching JDBC exceptions and translating them to the generic, more informative, exception hierarchy defined in the com.interface21.dao package.
Code using this class need only implement callback interfaces, giving them a clearly defined contract. The PreparedStatementCreator callback interface creates a prepared statement given a Connection provided by this class, providing SQL and any necessary parameters. The RowCallbackHandler interface extracts values from each row of a ResultSet.
Can be used within a service implementation via direct instantiation with a DataSource reference, or get prepared in an application context and given to services as bean reference. Note: The DataSource should always be configured as bean in the application context, in the first case given to the service directly, in the second case to the prepared template.
The motivation and design of this class is discussed in detail in Expert One-On-One J2EE Design and Development by Rod Johnson (Wrox, 2002).
Because this class is parameterizable by the callback interfaces and the SQLExceptionTranslater interface, it isn't necessary to subclass it. All SQL issued by this class is logged.
JndiObjectFactoryBean| Field Summary | |
protected org.apache.commons.logging.Log |
logger
|
static PreparedStatementSetter |
PREPARE_STATEMENT
Constant for use as a parameter to query methods to force use of a PreparedStatement rather than a Statement, even when there are no bind parameters. |
| Constructor Summary | |
JdbcTemplate()
Construct a new JdbcTemplate. |
|
JdbcTemplate(javax.sql.DataSource dataSource)
Construct a new JdbcTemplate, given a DataSource to obtain connections from. |
|
| Method Summary | |
void |
afterPropertiesSet()
Invoked by a BeanFactory after it has set all bean properties supplied. |
int[] |
batchUpdate(java.lang.String sql,
BatchPreparedStatementSetter setter)
Issue multiple updates using JDBC 2.0 batch updates and PreparedStatementSetters to set values on a PreparedStatement created by this method |
void |
doWithResultSetFromPreparedQuery(PreparedStatementCreator psc,
ResultSetExtracter rse)
Query using a prepared statement. |
void |
doWithResultSetFromStaticQuery(java.lang.String sql,
ResultSetExtracter rse)
Execute a query given static SQL. |
javax.sql.DataSource |
getDataSource()
Return the DataSource used by this template. |
protected SQLExceptionTranslater |
getExceptionTranslater()
Return the exception translater for this instance. |
boolean |
getIgnoreWarnings()
Return whether or not we ignore SQLWarnings. |
void |
query(PreparedStatementCreator psc,
RowCallbackHandler callbackHandler)
Query using a prepared statement. |
void |
query(java.lang.String sql,
PreparedStatementSetter pss,
RowCallbackHandler callbackHandler)
Query given SQL to create a prepared statement from SQL and a PreparedStatementSetter implementation that knows how to bind values to the query. |
void |
query(java.lang.String sql,
RowCallbackHandler callbackHandler)
Execute a query given static SQL. |
void |
setDataSource(javax.sql.DataSource dataSource)
Set the J2EE DataSource to obtain connections from. |
void |
setExceptionTranslater(SQLExceptionTranslater exceptionTranslater)
Set the exception translater used in this class. |
void |
setIgnoreWarnings(boolean ignoreWarnings)
Set whether or not we want to ignore SQLWarnings. |
int |
update(PreparedStatementCreator psc)
Issue an update using a PreparedStatementCreator to provide SQL and any required parameters |
int[] |
update(PreparedStatementCreator[] pscs)
Issue multiple updates using multiple PreparedStatementCreators to provide SQL and any required parameters |
int |
update(java.lang.String sql)
Issue a single SQL update. |
int |
update(java.lang.String sql,
PreparedStatementSetter pss)
Issue an update using a PreparedStatementSetter to set bind parameters, with given SQL. |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
public static final PreparedStatementSetter PREPARE_STATEMENT
protected final org.apache.commons.logging.Log logger
| Constructor Detail |
public JdbcTemplate()
setDataSource(javax.sql.DataSource)
public JdbcTemplate(javax.sql.DataSource dataSource)
throws InvalidParameterException
dataSource - J2EE DataSource to obtain connections fromInvalidParameterException - when dataSource is null| Method Detail |
public void setDataSource(javax.sql.DataSource dataSource)
throws InvalidParameterException
public javax.sql.DataSource getDataSource()
public void setIgnoreWarnings(boolean ignoreWarnings)
public boolean getIgnoreWarnings()
public void setExceptionTranslater(SQLExceptionTranslater exceptionTranslater)
exceptionTranslater - custom exception translatorprotected SQLExceptionTranslater getExceptionTranslater()
public void afterPropertiesSet()
InitializingBeanafterPropertiesSet in interface InitializingBeancom.interface21.beans.factory.InitializingBeanjava.lang.Exception - in the event of misconfiguration (such
as failure to set an essential property) or if initialization fails.
public void query(java.lang.String sql,
RowCallbackHandler callbackHandler)
throws DataAccessException
sql - SQL query to executecallbackHandler - object that will extract resultsDataAccessException - if there is any problem executing
the query
public void doWithResultSetFromStaticQuery(java.lang.String sql,
ResultSetExtracter rse)
throws DataAccessException
sql - SQL query to executerse - object that will extract all rows of resultsDataAccessException - if there is any problem executing
the query
public void query(PreparedStatementCreator psc,
RowCallbackHandler callbackHandler)
throws DataAccessException
psc - Callback handler that can create a PreparedStatement
given a ConnectioncallbackHandler - object that will extract results,
one row at a timeDataAccessException - if there is any problem
public void doWithResultSetFromPreparedQuery(PreparedStatementCreator psc,
ResultSetExtracter rse)
throws DataAccessException
psc - Callback handler that can create a PreparedStatement
given a Connectionrse - object that will extract results.DataAccessException - if there is any problem
public void query(java.lang.String sql,
PreparedStatementSetter pss,
RowCallbackHandler callbackHandler)
throws DataAccessException
sql - SQL to executepss - object that knows how to set values on the prepared statement.
If this is null, the SQL will be assumed to contain no bind parameters.
Even if there are no bind parameters, this object may be used to
set fetch size and other performance options.callbackHandler - object that will extract resultsDataAccessException - if the query fails
public int update(java.lang.String sql)
throws DataAccessException
sql - static SQL to executeDataAccessException - if there is any problem.
public int update(PreparedStatementCreator psc)
throws DataAccessException
psc - helper: callback object that provides SQL and any necessary parametersDataAccessException - if there is any problem issuing the update
public int[] update(PreparedStatementCreator[] pscs)
throws DataAccessException
pscs - array of helpers: callback object that provides SQL and any necessary parametersDataAccessException - if there is any problem issuing the update
public int update(java.lang.String sql,
PreparedStatementSetter pss)
throws DataAccessException
sql - SQL, containing bind parameterspss - helper that sets bind parameters. If this is null
we run an update with static SQLDataAccessException - if there is any problem issuing the update
TODO add a similar query method
public int[] batchUpdate(java.lang.String sql,
BatchPreparedStatementSetter setter)
throws DataAccessException
sql - defining PreparedStatement that will be reused.
All statements in the batch will use the same SQL.setter - object to set parameters on the
PreparedStatement created by this methodDataAccessException - if there is any problem issuing the update
|
[Deprecated API] | ||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | CURRENT API | |||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||