| java.lang.Object org.springframework.jdbc.support.JdbcAccessor org.springframework.jdbc.core.JdbcTemplate
JdbcTemplate | public class JdbcTemplate extends JdbcAccessor implements JdbcOperations(Code) | | This is the central class in the JDBC core 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, initiating
iteration over ResultSets and catching JDBC exceptions and translating
them to the generic, more informative exception hierarchy defined in the
org.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, providing SQL and
any necessary parameters. The
ResultSetExtractor interface extracts
values from a ResultSet. See also
PreparedStatementSetter and
RowMapper for two popular alternative callback interfaces.
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.
Because this class is parameterizable by the callback interfaces and
the
org.springframework.jdbc.support.SQLExceptionTranslator interface, there should be no need to subclass it.
All SQL operations performed by this class are logged at debug level,
using "org.springframework.jdbc.core.JdbcTemplate" as log category.
author: Rod Johnson author: Juergen Hoeller author: Thomas Risberg since: May 3, 2001 See Also: PreparedStatementCreator See Also: PreparedStatementSetter See Also: CallableStatementCreator See Also: PreparedStatementCallback See Also: CallableStatementCallback See Also: ResultSetExtractor See Also: RowCallbackHandler See Also: RowMapper See Also: org.springframework.jdbc.support.SQLExceptionTranslator |
Constructor Summary | |
public | JdbcTemplate() Construct a new JdbcTemplate for bean usage. | public | JdbcTemplate(DataSource dataSource) Construct a new JdbcTemplate, given a DataSource to obtain connections from. | public | JdbcTemplate(DataSource dataSource, boolean lazyInit) Construct a new JdbcTemplate, given a DataSource to obtain connections from. |
Method Summary | |
protected void | applyStatementSettings(Statement stmt) Prepare the given JDBC Statement (or PreparedStatement or CallableStatement),
applying statement settings such as fetch size, max rows, and query timeout. | public int[] | batchUpdate(String[] sql) | public int[] | batchUpdate(String sql, BatchPreparedStatementSetter pss) | public Map | call(CallableStatementCreator csc, List declaredParameters) | protected Connection | createConnectionProxy(Connection con) Create a close-suppressing proxy for the given JDBC Connection. | protected Map | createResultsMap() Create a Map instance to be used as results map. | public Object | execute(ConnectionCallback action) | public Object | execute(StatementCallback action) | public void | execute(String sql) | public Object | execute(PreparedStatementCreator psc, PreparedStatementCallback action) | public Object | execute(String sql, PreparedStatementCallback action) | public Object | execute(CallableStatementCreator csc, CallableStatementCallback action) | public Object | execute(String callString, CallableStatementCallback action) | protected Map | extractOutputParameters(CallableStatement cs, List parameters) Extract output parameters from the completed stored procedure. | protected Map | extractReturnedResults(CallableStatement cs, List updateCountParameters, List resultSetParameters, int updateCount) Extract returned ResultSets from the completed stored procedure. | protected RowMapper | getColumnMapRowMapper() Create a new RowMapper for reading columns as key-value pairs. | public int | getFetchSize() Return the fetch size specified for this JdbcTemplate. | public int | getMaxRows() Return the maximum number of rows specified for this JdbcTemplate. | public NativeJdbcExtractor | getNativeJdbcExtractor() Return the current NativeJdbcExtractor implementation. | public int | getQueryTimeout() Return the query timeout for statements that this JdbcTemplate executes. | protected RowMapper | getSingleColumnRowMapper(Class requiredType) Create a new RowMapper for reading result objects from a single column. | protected void | handleWarnings(SQLWarning warning) Throw an SQLWarningException if we're not ignoring warnings,
else log the warnings (at debug level). | public boolean | isIgnoreWarnings() Return whether or not we ignore SQLWarnings. | public boolean | isResultsMapCaseInsensitive() Return whether execution of a CallableStatement will return the results in a Map
that uses case insensitive names for the parameters. | public boolean | isSkipResultsProcessing() Return whether results processing should be skipped. | protected Map | processResultSet(ResultSet rs, ResultSetSupportingSqlParameter param) Process the given ResultSet from a stored procedure. | public Object | query(String sql, ResultSetExtractor rse) | public void | query(String sql, RowCallbackHandler rch) | public List | query(String sql, RowMapper rowMapper) | public Object | query(PreparedStatementCreator psc, PreparedStatementSetter pss, ResultSetExtractor rse) Query using a prepared statement, allowing for a PreparedStatementCreator
and a PreparedStatementSetter. | public Object | query(PreparedStatementCreator psc, ResultSetExtractor rse) | public Object | query(String sql, PreparedStatementSetter pss, ResultSetExtractor rse) | public Object | query(String sql, Object[] args, int[] argTypes, ResultSetExtractor rse) | public Object | query(String sql, Object[] args, ResultSetExtractor rse) | public void | query(PreparedStatementCreator psc, RowCallbackHandler rch) | public void | query(String sql, PreparedStatementSetter pss, RowCallbackHandler rch) | public void | query(String sql, Object[] args, int[] argTypes, RowCallbackHandler rch) | public void | query(String sql, Object[] args, RowCallbackHandler rch) | public List | query(PreparedStatementCreator psc, RowMapper rowMapper) | public List | query(String sql, PreparedStatementSetter pss, RowMapper rowMapper) | public List | query(String sql, Object[] args, int[] argTypes, RowMapper rowMapper) | public List | query(String sql, Object[] args, RowMapper rowMapper) | public int | queryForInt(String sql) | public int | queryForInt(String sql, Object[] args, int[] argTypes) | public int | queryForInt(String sql, Object[] args) | public List | queryForList(String sql, Class elementType) | public List | queryForList(String sql) | public List | queryForList(String sql, Object[] args, int[] argTypes, Class elementType) | public List | queryForList(String sql, Object[] args, Class elementType) | public List | queryForList(String sql, Object[] args, int[] argTypes) | public List | queryForList(String sql, Object[] args) | public long | queryForLong(String sql) | public long | queryForLong(String sql, Object[] args, int[] argTypes) | public long | queryForLong(String sql, Object[] args) | public Map | queryForMap(String sql) | public Map | queryForMap(String sql, Object[] args, int[] argTypes) | public Map | queryForMap(String sql, Object[] args) | public Object | queryForObject(String sql, RowMapper rowMapper) | public Object | queryForObject(String sql, Class requiredType) | public Object | queryForObject(String sql, Object[] args, int[] argTypes, RowMapper rowMapper) | public Object | queryForObject(String sql, Object[] args, RowMapper rowMapper) | public Object | queryForObject(String sql, Object[] args, int[] argTypes, Class requiredType) | public Object | queryForObject(String sql, Object[] args, Class requiredType) | public SqlRowSet | queryForRowSet(String sql) | public SqlRowSet | queryForRowSet(String sql, Object[] args, int[] argTypes) | public SqlRowSet | queryForRowSet(String sql, Object[] args) | public void | setFetchSize(int fetchSize) Set the fetch size for this JdbcTemplate. | public void | setIgnoreWarnings(boolean ignoreWarnings) Set whether or not we want to ignore SQLWarnings.
Default is "true", swallowing and logging all warnings. | public void | setMaxRows(int maxRows) Set the maximum number of rows for this JdbcTemplate. | public void | setNativeJdbcExtractor(NativeJdbcExtractor extractor) Set a NativeJdbcExtractor to extract native JDBC objects from wrapped handles. | public void | setQueryTimeout(int queryTimeout) Set the query timeout for statements that this JdbcTemplate executes. | public void | setResultsMapCaseInsensitive(boolean resultsMapCaseInsensitive) | public void | setSkipResultsProcessing(boolean skipResultsProcessing) Set whether results processing should be skipped. | public int | update(String sql) | protected int | update(PreparedStatementCreator psc, PreparedStatementSetter pss) | public int | update(PreparedStatementCreator psc) | public int | update(PreparedStatementCreator psc, KeyHolder generatedKeyHolder) | public int | update(String sql, PreparedStatementSetter pss) | public int | update(String sql, Object[] args, int[] argTypes) | public int | update(String sql, Object[] args) |
JdbcTemplate | public JdbcTemplate()(Code) | | Construct a new JdbcTemplate for bean usage.
Note: The DataSource has to be set before using the instance.
See Also: JdbcTemplate.setDataSource |
JdbcTemplate | public JdbcTemplate(DataSource dataSource)(Code) | | Construct a new JdbcTemplate, given a DataSource to obtain connections from.
Note: This will not trigger initialization of the exception translator.
Parameters: dataSource - the JDBC DataSource to obtain connections from |
JdbcTemplate | public JdbcTemplate(DataSource dataSource, boolean lazyInit)(Code) | | 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 - the JDBC DataSource to obtain connections from Parameters: lazyInit - whether to lazily initialize the SQLExceptionTranslator |
extractOutputParameters | protected Map extractOutputParameters(CallableStatement cs, List parameters) throws SQLException(Code) | | Extract output parameters from the completed stored procedure.
Parameters: cs - JDBC wrapper for the stored procedure Parameters: parameters - parameter list for the stored procedure parameters to the stored procedure Map that contains returned results |
extractReturnedResults | protected Map extractReturnedResults(CallableStatement cs, List updateCountParameters, List resultSetParameters, int updateCount) throws SQLException(Code) | | Extract returned ResultSets from the completed stored procedure.
Parameters: cs - JDBC wrapper for the stored procedure Parameters: updateCountParameters - Parameter list of declared update count parameters for the stored procedure Parameters: resultSetParameters - Parameter list of declared resturn resultSet parameters for the stored procedure Map that contains returned results |
getColumnMapRowMapper | protected RowMapper getColumnMapRowMapper()(Code) | | Create a new RowMapper for reading columns as key-value pairs.
the RowMapper to use See Also: ColumnMapRowMapper |
getFetchSize | public int getFetchSize()(Code) | | Return the fetch size specified for this JdbcTemplate.
|
getMaxRows | public int getMaxRows()(Code) | | Return the maximum number of rows specified for this JdbcTemplate.
|
getNativeJdbcExtractor | public NativeJdbcExtractor getNativeJdbcExtractor()(Code) | | Return the current NativeJdbcExtractor implementation.
|
getQueryTimeout | public int getQueryTimeout()(Code) | | Return the query timeout for statements that this JdbcTemplate executes.
|
getSingleColumnRowMapper | protected RowMapper getSingleColumnRowMapper(Class requiredType)(Code) | | Create a new RowMapper for reading result objects from a single column.
Parameters: requiredType - the type that each result object is expected to match the RowMapper to use See Also: SingleColumnRowMapper |
isIgnoreWarnings | public boolean isIgnoreWarnings()(Code) | | Return whether or not we ignore SQLWarnings.
|
isResultsMapCaseInsensitive | public boolean isResultsMapCaseInsensitive()(Code) | | Return whether execution of a CallableStatement will return the results in a Map
that uses case insensitive names for the parameters.
|
isSkipResultsProcessing | public boolean isSkipResultsProcessing()(Code) | | Return whether results processing should be skipped.
|
processResultSet | protected Map processResultSet(ResultSet rs, ResultSetSupportingSqlParameter param) throws SQLException(Code) | | Process the given ResultSet from a stored procedure.
Parameters: rs - the ResultSet to process Parameters: param - the corresponding stored procedure parameter Map that contains returned results |
query | public Object query(PreparedStatementCreator psc, PreparedStatementSetter pss, ResultSetExtractor rse) throws DataAccessException(Code) | | 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 aConnection Parameters: pss - 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. Parameters: rse - object that will extract results. an arbitrary result object, as returned by the ResultSetExtractor throws: DataAccessException - if there is any problem |
setFetchSize | public void setFetchSize(int fetchSize)(Code) | | 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.
See Also: java.sql.Statement.setFetchSize |
setMaxRows | public void setMaxRows(int maxRows)(Code) | | 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.
See Also: java.sql.Statement.setMaxRows |
setNativeJdbcExtractor | public void setNativeJdbcExtractor(NativeJdbcExtractor extractor)(Code) | | 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).
|
setQueryTimeout | public void setQueryTimeout(int queryTimeout)(Code) | | Set the query timeout for statements that this JdbcTemplate executes.
Default is 0, indicating to use the JDBC driver's default.
Note: Any timeout specified here will be overridden by the remaining
transaction timeout when executing within a transaction that has a
timeout specified at the transaction level.
See Also: java.sql.Statement.setQueryTimeout |
setResultsMapCaseInsensitive | public void setResultsMapCaseInsensitive(boolean resultsMapCaseInsensitive)(Code) | | |
setSkipResultsProcessing | public void setSkipResultsProcessing(boolean skipResultsProcessing)(Code) | | Set whether results processing should be skipped. Can be used to optimize callable
statement processing when we know that no results are being passed back - the processing
of out parameter will still take place. This can be used to avoid a bug in some older
Oracle JDBC drivers like 10.1.0.2.
|
Fields inherited from org.springframework.jdbc.support.JdbcAccessor | final protected Log logger(Code)(Java Doc)
|
|
|