Save This Page
Home » spring-framework-2.5.6-with-dependencies » org.springframework » jdbc » core » [javadoc | source]
org.springframework.jdbc.core
public class: JdbcTemplate [javadoc | source]
java.lang.Object
   org.springframework.jdbc.support.JdbcAccessor
      org.springframework.jdbc.core.JdbcTemplate

All Implemented Interfaces:
    JdbcOperations, InitializingBean

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.

Fields inherited from org.springframework.jdbc.support.JdbcAccessor:
logger
Constructor:
 public JdbcTemplate() 
 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 - the JDBC DataSource to obtain connections from
 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 - the JDBC DataSource to obtain connections from
    lazyInit - whether to lazily initialize the SQLExceptionTranslator
Method from org.springframework.jdbc.core.JdbcTemplate Summary:
applyStatementSettings,   batchUpdate,   batchUpdate,   call,   createConnectionProxy,   createResultsMap,   execute,   execute,   execute,   execute,   execute,   execute,   execute,   extractOutputParameters,   extractReturnedResults,   getColumnMapRowMapper,   getFetchSize,   getMaxRows,   getNativeJdbcExtractor,   getQueryTimeout,   getSingleColumnRowMapper,   handleWarnings,   handleWarnings,   isIgnoreWarnings,   isResultsMapCaseInsensitive,   isSkipResultsProcessing,   isSkipUndeclaredResults,   processResultSet,   query,   query,   query,   query,   query,   query,   query,   query,   query,   query,   query,   query,   query,   query,   query,   query,   queryForInt,   queryForInt,   queryForInt,   queryForList,   queryForList,   queryForList,   queryForList,   queryForList,   queryForList,   queryForLong,   queryForLong,   queryForLong,   queryForMap,   queryForMap,   queryForMap,   queryForObject,   queryForObject,   queryForObject,   queryForObject,   queryForObject,   queryForObject,   queryForRowSet,   queryForRowSet,   queryForRowSet,   setFetchSize,   setIgnoreWarnings,   setMaxRows,   setNativeJdbcExtractor,   setQueryTimeout,   setResultsMapCaseInsensitive,   setSkipResultsProcessing,   setSkipUndeclaredResults,   update,   update,   update,   update,   update,   update,   update
Methods from org.springframework.jdbc.support.JdbcAccessor:
afterPropertiesSet,   getDataSource,   getExceptionTranslator,   isLazyInit,   setDataSource,   setDatabaseProductName,   setExceptionTranslator,   setLazyInit
Methods from java.lang.Object:
equals,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from org.springframework.jdbc.core.JdbcTemplate Detail:
 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.
 public int[] batchUpdate(String[] sql) throws DataAccessException 
 public int[] batchUpdate(String sql,
    BatchPreparedStatementSetter pss) throws DataAccessException 
 public Map call(CallableStatementCreator csc,
    List declaredParameters) throws DataAccessException 
 protected Connection createConnectionProxy(Connection con) 
    Create a close-suppressing proxy for the given JDBC Connection. Called by the execute method.

    The proxy also prepares returned JDBC Statements, applying statement settings such as fetch size, max rows, and query timeout.

 protected Map createResultsMap() 
    Create a Map instance to be used as results map.

    If "isResultsMapCaseInsensitive" has been set to true, a linked case-insensitive Map will be created if possible, else a plain HashMap (see Spring's CollectionFactory).

 public Object execute(ConnectionCallback action) throws DataAccessException 
 public Object execute(StatementCallback action) throws DataAccessException 
 public  void execute(String sql) throws DataAccessException 
 public Object execute(PreparedStatementCreator psc,
    PreparedStatementCallback action) throws DataAccessException 
 public Object execute(String sql,
    PreparedStatementCallback action) throws DataAccessException 
 public Object execute(CallableStatementCreator csc,
    CallableStatementCallback action) throws DataAccessException 
 public Object execute(String callString,
    CallableStatementCallback action) throws DataAccessException 
 protected Map extractOutputParameters(CallableStatement cs,
    List parameters) throws SQLException 
    Extract output parameters from the completed stored procedure.
 protected Map extractReturnedResults(CallableStatement cs,
    List updateCountParameters,
    List resultSetParameters,
    int updateCount) throws SQLException 
    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(Statement stmt) throws SQLException 
    Throw an SQLWarningException if we're not ignoring warnings, else log the warnings (at debug level).
 protected  void handleWarnings(SQLWarning warning) throws SQLWarningException 
    Throw an SQLWarningException if encountering an actual warning.
 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.
 public boolean isSkipUndeclaredResults() 
    Return whether undeclared results should be skipped.
 protected Map processResultSet(ResultSet rs,
    ResultSetSupportingSqlParameter param) throws SQLException 
    Process the given ResultSet from a stored procedure.
 public Object query(String sql,
    ResultSetExtractor rse) throws DataAccessException 
 public  void query(String sql,
    RowCallbackHandler rch) throws DataAccessException 
 public List query(String sql,
    RowMapper rowMapper) throws DataAccessException 
 public Object query(PreparedStatementCreator psc,
    ResultSetExtractor rse) throws DataAccessException 
 public  void query(PreparedStatementCreator psc,
    RowCallbackHandler rch) throws DataAccessException 
 public List query(PreparedStatementCreator psc,
    RowMapper rowMapper) throws DataAccessException 
 public 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.
 public Object query(String sql,
    PreparedStatementSetter pss,
    ResultSetExtractor rse) throws DataAccessException 
 public Object query(String sql,
    Object[] args,
    ResultSetExtractor rse) throws DataAccessException 
 public  void query(String sql,
    PreparedStatementSetter pss,
    RowCallbackHandler rch) throws DataAccessException 
 public  void query(String sql,
    Object[] args,
    RowCallbackHandler rch) throws DataAccessException 
 public List query(String sql,
    PreparedStatementSetter pss,
    RowMapper rowMapper) throws DataAccessException 
 public List query(String sql,
    Object[] args,
    RowMapper rowMapper) throws DataAccessException 
 public Object query(String sql,
    Object[] args,
    int[] argTypes,
    ResultSetExtractor rse) throws DataAccessException 
 public  void query(String sql,
    Object[] args,
    int[] argTypes,
    RowCallbackHandler rch) throws DataAccessException 
 public List query(String sql,
    Object[] args,
    int[] argTypes,
    RowMapper rowMapper) throws DataAccessException 
 public int queryForInt(String sql) throws DataAccessException 
 public int queryForInt(String sql,
    Object[] args) throws DataAccessException 
 public int queryForInt(String sql,
    Object[] args,
    int[] argTypes) throws DataAccessException 
 public List queryForList(String sql) throws DataAccessException 
 public List queryForList(String sql,
    Class elementType) throws DataAccessException 
 public List queryForList(String sql,
    Object[] args) throws DataAccessException 
 public List queryForList(String sql,
    Object[] args,
    Class elementType) throws DataAccessException 
 public List queryForList(String sql,
    Object[] args,
    int[] argTypes) throws DataAccessException 
 public List queryForList(String sql,
    Object[] args,
    int[] argTypes,
    Class elementType) throws DataAccessException 
 public long queryForLong(String sql) throws DataAccessException 
 public long queryForLong(String sql,
    Object[] args) throws DataAccessException 
 public long queryForLong(String sql,
    Object[] args,
    int[] argTypes) throws DataAccessException 
 public Map queryForMap(String sql) throws DataAccessException 
 public Map queryForMap(String sql,
    Object[] args) throws DataAccessException 
 public Map queryForMap(String sql,
    Object[] args,
    int[] argTypes) throws DataAccessException 
 public Object queryForObject(String sql,
    RowMapper rowMapper) throws DataAccessException 
 public Object queryForObject(String sql,
    Class requiredType) throws DataAccessException 
 public Object queryForObject(String sql,
    Object[] args,
    RowMapper rowMapper) throws DataAccessException 
 public Object queryForObject(String sql,
    Object[] args,
    Class requiredType) throws DataAccessException 
 public Object queryForObject(String sql,
    Object[] args,
    int[] argTypes,
    RowMapper rowMapper) throws DataAccessException 
 public Object queryForObject(String sql,
    Object[] args,
    int[] argTypes,
    Class requiredType) throws DataAccessException 
 public SqlRowSet queryForRowSet(String sql) throws DataAccessException 
 public SqlRowSet queryForRowSet(String sql,
    Object[] args) throws DataAccessException 
 public SqlRowSet queryForRowSet(String sql,
    Object[] args,
    int[] argTypes) throws DataAccessException 
 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", swallowing and logging all warnings. Switch this flag to "false" to make the JdbcTemplate throw a SQLWarningException instead.

 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  void setQueryTimeout(int queryTimeout) 
    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.

 public  void setResultsMapCaseInsensitive(boolean resultsMapCaseInsensitive) 
    Set whether execution of a CallableStatement will return the results in a Map that uses case insensitive names for the parameters.
 public  void setSkipResultsProcessing(boolean skipResultsProcessing) 
    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.
 public  void setSkipUndeclaredResults(boolean skipUndeclaredResults) 
    Set whether undelared results should be skipped.
 public int update(String sql) throws DataAccessException 
 public int update(PreparedStatementCreator psc) throws DataAccessException 
 protected int update(PreparedStatementCreator psc,
    PreparedStatementSetter pss) throws DataAccessException 
 public int update(PreparedStatementCreator psc,
    KeyHolder generatedKeyHolder) throws DataAccessException 
 public int update(String sql,
    PreparedStatementSetter pss) throws DataAccessException 
 public int update(String sql,
    Object[] args) throws DataAccessException 
 public int update(String sql,
    Object[] args,
    int[] argTypes) throws DataAccessException