| Java Doc By Examples | |
| Prev Class | Next Class | Frames | No Frames |
| Summary: Nested | Field | Method | Constr | Detail: Nested | Field | Method | Constr |
java.lang.Objectorg.springframework.jdbc.support.JdbcAccessororg.springframework.jdbc.core.JdbcTemplateorg.springframework.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 a 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 SQLExceptionTranslator interface, it isn't necessary to subclass it.
All operations performed by this class are logged at debug level.
ResultSetExtractor, RowCallbackHandler, RowMapper, org.springframework.dao, org.springframework.jdbc.datasource, org.springframework.jdbc.objectField Summary |
Fields inherited from class org.springframework.jdbc.support.JdbcAccessor | |
logger | |
Constructor Summary | |
| |
| |
| |
Method Summary | |
protected void |
|
int[] |
|
int[] |
|
Map<K,V> |
|
protected Connection |
|
void | |
Object |
|
Object |
|
Object |
|
Object |
|
Object |
|
Object |
|
protected Map<K,V> |
|
protected Map<K,V> |
|
protected RowMapper |
|
int |
|
int |
|
NativeJdbcExtractor |
|
protected RowMapper |
|
boolean |
|
protected Map<K,V> |
|
Object |
|
void |
|
List<E> | |
Object |
|
void |
|
List<E> | |
Object |
|
void |
|
List<E> |
|
Object |
|
void |
|
List<E> | |
protected Object |
|
Object |
|
void |
|
List<E> |
|
int |
|
int |
|
int |
|
List<E> |
|
List<E> |
|
List<E> |
|
List<E> |
|
List<E> |
|
List<E> |
|
long |
|
long |
|
long |
|
Map<K,V> |
|
Map<K,V> |
|
Map<K,V> |
|
Object |
|
Object |
|
Object |
|
Object |
|
Object |
|
Object |
|
SqlRowSet |
|
SqlRowSet |
|
SqlRowSet |
|
void |
|
void |
|
void |
|
void |
|
int | |
int | |
int | |
int |
|
int | |
protected int | |
int |
|
Methods inherited from class org.springframework.jdbc.support.JdbcAccessor | |
afterPropertiesSet, getDataSource, getExceptionTranslator, isLazyInit, setDataSource, setDatabaseProductName, setExceptionTranslator, setLazyInit | |
Methods inherited from class java.lang.Object | |
clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait | |
public JdbcTemplate()
Construct a new JdbcTemplate for bean usage. Note: The DataSource has to be set before using the instance. This constructor can be used to prepare a JdbcTemplate via a BeanFactory, typically setting the DataSource via setDataSource.
- See Also:
JdbcTemplate
1: public void testJdbcDaoSupportWithJdbcTemplate() throws Exception { 2: JdbcTemplate template = new JdbcTemplate(); 3: final List test = new ArrayList(); 4: JdbcDaoSupport dao = new JdbcDaoSupport() { 5: protected void initDao() {
1: final ArrayList results = new ArrayList(); 2: JdbcTemplate template = new JdbcTemplate(); 3: template.setDataSource(this.getDataSource()); 4: ... 5: final Bike bike = new Bike(); 6: JdbcTemplate template = new JdbcTemplate(); 7: template.setDataSource(this.getDataSource()); 8: ... 9: final Bike bike = new Bike(); 10: JdbcTemplate template = new JdbcTemplate(); 11: template.setDataSource(this.getDataSource());
1: public void initBase() { 2: JdbcTemplate tpl = new JdbcTemplate(); 3: tpl.setDataSource(this.getDataSource()); 4: ... 5: try { 6: JdbcTemplate tpl = new JdbcTemplate(); 7: tpl.setDataSource(this.getDataSource()); 8: tpl.batchUpdate(INSERT_SQL, new BatchSetter(countries, locale)); 9: } catch (DataAccessException e) {
public JdbcTemplate(DataSource dataSource)
Construct a new JdbcTemplate, given a DataSource to obtain connections from. Note: This will not trigger initialization of the exception translator.
- Parameters:
dataSource- JDBC DataSource to obtain connections from
1: 2: JdbcTemplate jt = new JdbcTemplate(dataSource); 3: jt.execute("delete from mytable"); 4: jt.execute("insert into mytable (id, name) values(1, 'John')"); 5: jt.execute("insert into mytable (id, name) values(2, 'Jane')");
1: context.getBean("dataSource"); 2: JdbcTemplate jdbcTemplate = new JdbcTemplate(datsSource); 3: 4: 5: jdbcTemplate.execute("select * from pixuser");
1: protected JdbcTemplate createJdbcTemplate(DataSource dataSource) { 2: JdbcTemplate jt = new JdbcTemplate(dataSource); 3: this.simpleJdbcTemplate = new SimpleJdbcTemplate(jt); 4: return jt; 5: }
1: public final void setDataSource(final DataSource dataSource) { 2: this.jdbcTemplate = new JdbcTemplate(dataSource); 3: } 4: 5: protected final JdbcTemplate getJdbcTemplate() {
1: public void testCallReserveSeat() { 2: JdbcTemplate jt = new JdbcTemplate(ds); 3: jt.execute("delete from seat_status"); 4: ... 5: public void testCallReserveSeats() { 6: JdbcTemplate jt = new JdbcTemplate(ds); 7: jt.execute("delete from seat_status"); 8: Integer[] numarr = {new Integer(2), new Integer(3)}; 9: CallReserveSeats proc = new CallReserveSeats(ds);
public JdbcTemplate(DataSource dataSource, boolean lazyInit)
Construct a new JdbcTemplate, given a DataSource to obtain connections from. Note: Depending on the "lazyInit" flag, initialization of the exception translator will be triggered.
- Parameters:
dataSource- JDBC DataSource to obtain connections fromlazyInit- whether to lazily initialize the SQLExceptionTranslator
protected void applyStatementSettings(Statement stmt) throws SQLException
Prepare the given JDBC Statement (or PreparedStatement or CallableStatement), applying statement settings such as fetch size, max rows, and query timeout.
- Parameters:
stmt- the JDBC Statement to prepare
public int[] batchUpdate(String sql, BatchPreparedStatementSetter pss) throws DataAccessException
- Specified by:
- batchUpdate in interface JdbcOperations
public int[] batchUpdate(String[] sql) throws DataAccessException
- Specified by:
- batchUpdate in interface JdbcOperations
public Map<K,V> call(CallableStatementCreator csc, List<E> declaredParameters) throws DataAccessException
- Specified by:
- call in interface JdbcOperations
protected Connection createConnectionProxy(Connection con)
Create a close-suppressing proxy for the given JDBC Connection. Called by theexecutemethod. The proxy also prepares returned JDBC Statements, applying statement settings such as fetch size, max rows, and query timeout.
- Parameters:
con- the JDBC Connection to create a proxy for
- Returns:
- the Connection proxy
public void execute(String sql) throws DataAccessException
- Specified by:
- execute in interface JdbcOperations
public Object execute(String callString, CallableStatementCallback action) throws DataAccessException
- Specified by:
- execute in interface JdbcOperations
public Object execute(String sql, PreparedStatementCallback action) throws DataAccessException
- Specified by:
- execute in interface JdbcOperations
public Object execute(CallableStatementCreator csc, CallableStatementCallback action) throws DataAccessException
- Specified by:
- execute in interface JdbcOperations
public Object execute(ConnectionCallback action) throws DataAccessException
- Specified by:
- execute in interface JdbcOperations
public Object execute(PreparedStatementCreator psc, PreparedStatementCallback action) throws DataAccessException
- Specified by:
- execute in interface JdbcOperations
public Object execute(StatementCallback action) throws DataAccessException
- Specified by:
- execute in interface JdbcOperations
protected Map<K,V> extractOutputParameters(CallableStatement cs, List<E> parameters) throws SQLException
Extract output parameters from the completed stored procedure.
- Parameters:
cs- JDBC wrapper for the stored procedureparameters- parameter list for the stored procedure
- Returns:
- parameters to the stored procedure
protected Map<K,V> extractReturnedResultSets(CallableStatement cs, List<E> parameters, int updateCount) throws SQLException
Extract returned ResultSets from the completed stored procedure.
- Parameters:
cs- JDBC wrapper for the stored procedureparameters- Parameter list for the stored procedure
- Returns:
- Map that contains returned results
protected RowMapper getColumnMapRowMapper()
Create a new RowMapper for reading columns as key-value pairs.
- Returns:
- the RowMapper to use
- See Also:
ColumnMapRowMapper
public int getMaxRows()
Return the maximum number of rows specified for this JdbcTemplate.
public NativeJdbcExtractor getNativeJdbcExtractor()
Return the current NativeJdbcExtractor implementation.
protected RowMapper getSingleColumnRowMapper(Class<T> requiredType)
Create a new RowMapper for reading result objects from a single column.
- Parameters:
requiredType- the type that each result object is expected to match
- Returns:
- the RowMapper to use
- See Also:
SingleColumnRowMapper
public boolean isIgnoreWarnings()
Return whether or not we ignore SQLWarnings. Default is "true".
protected Map<K,V> processResultSet(ResultSet rs, ResultSetSupportingSqlParameter param) throws SQLException
Process the given ResultSet from a stored procedure.
- Parameters:
rs- the ResultSet to processparam- the corresponding stored procedure parameter
- Returns:
- Map that contains returned results
public Object query(String sql, Object[] args, int[] argTypes, ResultSetExtractor rse) throws DataAccessException
- Specified by:
- query in interface JdbcOperations
public void query(String sql, Object[] args, int[] argTypes, RowCallbackHandler rch) throws DataAccessException
- Specified by:
- query in interface JdbcOperations
public List<E> query(String sql, Object[] args, int[] argTypes, RowMapper rowMapper) throws DataAccessException
- Specified by:
- query in interface JdbcOperations
public Object query(String sql, Object[] args, ResultSetExtractor rse) throws DataAccessException
- Specified by:
- query in interface JdbcOperations
public void query(String sql, Object[] args, RowCallbackHandler rch) throws DataAccessException
- Specified by:
- query in interface JdbcOperations
public List<E> query(String sql, Object[] args, RowMapper rowMapper) throws DataAccessException
- Specified by:
- query in interface JdbcOperations
public Object query(String sql, PreparedStatementSetter pss, ResultSetExtractor rse) throws DataAccessException
- Specified by:
- query in interface JdbcOperations
public void query(String sql, PreparedStatementSetter pss, RowCallbackHandler rch) throws DataAccessException
- Specified by:
- query in interface JdbcOperations
public List<E> query(String sql, PreparedStatementSetter pss, RowMapper rowMapper) throws DataAccessException
- Specified by:
- query in interface JdbcOperations
public Object query(String sql, ResultSetExtractor rse) throws DataAccessException
- Specified by:
- query in interface JdbcOperations
public void query(String sql, RowCallbackHandler rch) throws DataAccessException
- Specified by:
- query in interface JdbcOperations
public List<E> query(String sql, RowMapper rowMapper) throws DataAccessException
- Specified by:
- query in interface JdbcOperations
protected Object query(PreparedStatementCreator psc, PreparedStatementSetter pss, ResultSetExtractor rse) throws DataAccessException
Query using a prepared statement, allowing for a PreparedStatementCreator and a PreparedStatementSetter. Most other query methods use this method, but application code will always work with either a creator or a setter.
- Parameters:
psc- Callback handler that can create a PreparedStatement given a Connectionpss- 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.rse- object that will extract results.
- Returns:
- an arbitrary result object, as returned by the ResultSetExtractor
- Throws:
DataAccessException- if there is any problem
public Object query(PreparedStatementCreator psc, ResultSetExtractor rse) throws DataAccessException
- Specified by:
- query in interface JdbcOperations
public void query(PreparedStatementCreator psc, RowCallbackHandler rch) throws DataAccessException
- Specified by:
- query in interface JdbcOperations
public List<E> query(PreparedStatementCreator psc, RowMapper rowMapper) throws DataAccessException
- Specified by:
- query in interface JdbcOperations
public int queryForInt(String sql) throws DataAccessException
- Specified by:
- queryForInt in interface JdbcOperations
public int queryForInt(String sql, Object[] args) throws DataAccessException
- Specified by:
- queryForInt in interface JdbcOperations
1: 2: private JdbcTemplate jdbcTemplate; 3: 4: ... 5: public BoxOfficeDBHelper(JdbcTemplate jdbcTemplate) { 6: this.jdbcTemplate = jdbcTemplate; 7: ... 8: Object[] args = new Object[] { new Long(seatId), new Long(bookingId) }; 9: return jdbcTemplate.queryForInt(sql, args) > 0; 10: } 11: ... 12: Object[] args = new Object[] { new Long(bookingId) }; 13: int count = jdbcTemplate.queryForInt(sql, args);
public int queryForInt(String sql, Object[] args, int[] argTypes) throws DataAccessException
- Specified by:
- queryForInt in interface JdbcOperations
public List<E> queryForList(String sql) throws DataAccessException
- Specified by:
- queryForList in interface JdbcOperations
public List<E> queryForList(String sql, Class<T> elementType) throws DataAccessException
- Specified by:
- queryForList in interface JdbcOperations
public List<E> queryForList(String sql, Object[] args) throws DataAccessException
- Specified by:
- queryForList in interface JdbcOperations
public List<E> queryForList(String sql, Object[] args, int[] argTypes) throws DataAccessException
- Specified by:
- queryForList in interface JdbcOperations
public List<E> queryForList(String sql, Object[] args, int[] argTypes, Class<T> elementType) throws DataAccessException
- Specified by:
- queryForList in interface JdbcOperations
public List<E> queryForList(String sql, Object[] args, Class<T> elementType) throws DataAccessException
- Specified by:
- queryForList in interface JdbcOperations
public long queryForLong(String sql) throws DataAccessException
- Specified by:
- queryForLong in interface JdbcOperations
public long queryForLong(String sql, Object[] args) throws DataAccessException
- Specified by:
- queryForLong in interface JdbcOperations
public long queryForLong(String sql, Object[] args, int[] argTypes) throws DataAccessException
- Specified by:
- queryForLong in interface JdbcOperations
public Map<K,V> queryForMap(String sql) throws DataAccessException
- Specified by:
- queryForMap in interface JdbcOperations
public Map<K,V> queryForMap(String sql, Object[] args) throws DataAccessException
- Specified by:
- queryForMap in interface JdbcOperations
public Map<K,V> queryForMap(String sql, Object[] args, int[] argTypes) throws DataAccessException
- Specified by:
- queryForMap in interface JdbcOperations
public Object queryForObject(String sql, Class<T> requiredType) throws DataAccessException
- Specified by:
- queryForObject in interface JdbcOperations
public Object queryForObject(String sql, Object[] args, int[] argTypes, Class<T> requiredType) throws DataAccessException
- Specified by:
- queryForObject in interface JdbcOperations
public Object queryForObject(String sql, Object[] args, int[] argTypes, RowMapper rowMapper) throws DataAccessException
- Specified by:
- queryForObject in interface JdbcOperations
public Object queryForObject(String sql, Object[] args, Class<T> requiredType) throws DataAccessException
- Specified by:
- queryForObject in interface JdbcOperations
public Object queryForObject(String sql, Object[] args, RowMapper rowMapper) throws DataAccessException
- Specified by:
- queryForObject in interface JdbcOperations
public Object queryForObject(String sql, RowMapper rowMapper) throws DataAccessException
- Specified by:
- queryForObject in interface JdbcOperations
public SqlRowSet queryForRowSet(String sql) throws DataAccessException
- Specified by:
- queryForRowSet in interface JdbcOperations
1: import org.apache.commons.logging.LogFactory; 2: import org.springframework.jdbc.core.JdbcTemplate; 3: import org.springframework.jdbc.support.rowset.SqlRowSet; 4: ... 5: List runSql(String sql){ 6: JdbcTemplate jt = new JdbcTemplate(ds); 7: ... 8: SqlRowSet rows = jt.queryForRowSet(sql); 9: List result = new ArrayList();
public SqlRowSet queryForRowSet(String sql, Object[] args) throws DataAccessException
- Specified by:
- queryForRowSet in interface JdbcOperations
public SqlRowSet queryForRowSet(String sql, Object[] args, int[] argTypes) throws DataAccessException
- Specified by:
- queryForRowSet in interface JdbcOperations
public void setFetchSize(int fetchSize)
Set the fetch size for this JdbcTemplate. This is important for processing large result sets: Setting this higher than the default value will increase processing speed at the cost of memory consumption; setting this lower can avoid transferring row data that will never be read by the application. Default is 0, indicating to use the JDBC driver's default.
public void setIgnoreWarnings(boolean ignoreWarnings)
Set whether or not we want to ignore SQLWarnings. Default is "true".
public void setMaxRows(int maxRows)
Set the maximum number of rows for this JdbcTemplate. This is important for processing subsets of large result sets, avoiding to read and hold the entire result set in the database or in the JDBC driver if we're never interested in the entire result in the first place (for example, when performing searches that might return a large number of matches). Default is 0, indicating to use the JDBC driver's default.
public void setNativeJdbcExtractor(NativeJdbcExtractor extractor)
Set a NativeJdbcExtractor to extract native JDBC objects from wrapped handles. Useful if native Statement and/or ResultSet handles are expected for casting to database-specific implementation classes, but a connection pool that wraps JDBC objects is used (note: any pool will return wrapped Connections).
public int update(String sql) throws DataAccessException
- Specified by:
- update in interface JdbcOperations
1: import org.springframework.jdbc.core.BatchPreparedStatementSetter; 2: import org.springframework.jdbc.core.JdbcTemplate; 3: import org.springframework.jdbc.core.SqlParameter; 4: ... 5: public void initBase() { 6: JdbcTemplate tpl = new JdbcTemplate(); 7: tpl.setDataSource(this.getDataSource()); 8: ... 9: try { 10: tpl.update(dropSql); 11: logger.info("'countries' table deleted"); 12: ... 13: 14: tpl.update(createSql);
public int update(String sql, Object[] args) throws DataAccessException
- Specified by:
- update in interface JdbcOperations
1: 2: import org.springframework.jdbc.core.JdbcTemplate; 3: import org.springframework.jdbc.core.RowMapper; 4: ... 5: 6: JdbcTemplate jt = getJdbcTemplate(); 7: 8: ... 9: 10: jt.update(INSERT_PIXUSER_SQL, userParameters); 11: 12: ... 13: }; 14: jt.update(INSERT_ALBUM_SQL, movieParameters);
1: 2: private JdbcTemplate jdbcTemplate; 3: 4: ... 5: public BoxOfficeDBHelper(JdbcTemplate jdbcTemplate) { 6: this.jdbcTemplate = jdbcTemplate; 7: ... 8: Object[] args = new Object[] { new Long(id), name }; 9: jdbcTemplate.update(sql, args); 10: } 11: ... 12: }; 13: jdbcTemplate.update(sql, args);
public int update(String sql, Object[] args, int[] argTypes) throws DataAccessException
- Specified by:
- update in interface JdbcOperations
public int update(String sql, PreparedStatementSetter pss) throws DataAccessException
- Specified by:
- update in interface JdbcOperations
1: 2: import org.springframework.jdbc.core.JdbcTemplate; 3: import org.springframework.jdbc.core.RowMapper; 4: ... 5: 6: JdbcTemplate jt = getJdbcTemplate(); 7: 8: ... 9: 10: jt.update(INSERT_PIXUSER_SQL, userParameters); 11: 12: ... 13: }; 14: jt.update(INSERT_ALBUM_SQL, movieParameters);
1: 2: private JdbcTemplate jdbcTemplate; 3: 4: ... 5: public BoxOfficeDBHelper(JdbcTemplate jdbcTemplate) { 6: this.jdbcTemplate = jdbcTemplate; 7: ... 8: Object[] args = new Object[] { new Long(id), name }; 9: jdbcTemplate.update(sql, args); 10: } 11: ... 12: }; 13: jdbcTemplate.update(sql, args);
public int update(PreparedStatementCreator psc) throws DataAccessException
- Specified by:
- update in interface JdbcOperations
1: import org.springframework.jdbc.core.BatchPreparedStatementSetter; 2: import org.springframework.jdbc.core.JdbcTemplate; 3: import org.springframework.jdbc.core.SqlParameter; 4: ... 5: public void initBase() { 6: JdbcTemplate tpl = new JdbcTemplate(); 7: tpl.setDataSource(this.getDataSource()); 8: ... 9: try { 10: tpl.update(dropSql); 11: logger.info("'countries' table deleted"); 12: ... 13: 14: tpl.update(createSql);
protected int update(PreparedStatementCreator psc, PreparedStatementSetter pss) throws DataAccessException
public int update(PreparedStatementCreator psc, KeyHolder generatedKeyHolder) throws DataAccessException
- Specified by:
- update in interface JdbcOperations
1: 2: import org.springframework.jdbc.core.JdbcTemplate; 3: import org.springframework.jdbc.core.RowMapper; 4: ... 5: 6: JdbcTemplate jt = getJdbcTemplate(); 7: 8: ... 9: 10: jt.update(INSERT_PIXUSER_SQL, userParameters); 11: 12: ... 13: }; 14: jt.update(INSERT_ALBUM_SQL, movieParameters);
1: 2: private JdbcTemplate jdbcTemplate; 3: 4: ... 5: public BoxOfficeDBHelper(JdbcTemplate jdbcTemplate) { 6: this.jdbcTemplate = jdbcTemplate; 7: ... 8: Object[] args = new Object[] { new Long(id), name }; 9: jdbcTemplate.update(sql, args); 10: } 11: ... 12: }; 13: jdbcTemplate.update(sql, args);