org.springframework.jdbc.core
Class JdbcTemplate

java.lang.Object
  extended byorg.springframework.jdbc.support.JdbcAccessor
      extended byorg.springframework.jdbc.core.JdbcTemplate
All Implemented Interfaces:
InitializingBean, JdbcOperations

public class JdbcTemplate
extends JdbcAccessor
implements 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 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 SQL issued by this class is logged.

Since:
May 3, 2001
Author:
Rod Johnson, Juergen Hoeller, Thomas Risberg
See Also:
ResultSetExtractor, RowCallbackHandler, RowMapper, org.springframework.dao, org.springframework.jdbc.datasource, org.springframework.jdbc.object

Field Summary
 
Fields inherited from class org.springframework.jdbc.support.JdbcAccessor
logger
 
Constructor Summary
JdbcTemplate()
          Construct a new JdbcTemplate for bean usage.
JdbcTemplate(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 (and satisfied BeanFactoryAware and ApplicationContextAware).
 int[] batchUpdate(String[] sql)
          Issue multiple SQL updates on a single Statement, using JDBC 2.0 batching.
 int[] batchUpdate(String sql, BatchPreparedStatementSetter pss)
          Issue multiple updates on a single PreparedStatement, using JDBC 2.0 batch updates and a BatchPreparedStatementSetter to set values.
 Map call(CallableStatementCreator csc, List declaredParameters)
          Execute a SQL call using a CallableStatementCreator to provide SQL and any required parameters.
 Object execute(CallableStatementCreator csc, CallableStatementCallback action)
          Execute the action specified by the given action object within a JDBC CallableStatement.
 Object execute(ConnectionCallback action)
          Execute the action specified by the given action object within a JDBC Connection.
 Object execute(PreparedStatementCreator psc, PreparedStatementCallback action)
          Execute the action specified by the given action object within a JDBC PreparedStatement.
 Object execute(StatementCallback action)
          Execute the action specified by the given action object within a JDBC Statement.
 void execute(String sql)
          Issue a single SQL execute, typically a DDL statement.
 Object execute(String callString, CallableStatementCallback action)
          Execute the action specified by the given action object within a JDBC CallableStatement.
 Object execute(String sql, PreparedStatementCallback action)
          Execute the action specified by the given action object within a JDBC PreparedStatement.
protected  Map extractOutputParameters(CallableStatement cs, List parameters)
          Extract output parameters from the completed stored procedure.
protected  Map extractReturnedResultSets(CallableStatement cs, List parameters, int updateCount)
          Extract returned ResultSets from the completed stored procedure.
 int getFetchSize()
          Return the fetch size specified for this JdbcTemplate.
 boolean getIgnoreWarnings()
          Deprecated. in favor of isIgnoreWarnings
 int getMaxRows()
          Return the maximum number of rows specified for this JdbcTemplate.
 NativeJdbcExtractor getNativeJdbcExtractor()
          Return the current NativeJdbcExtractor implementation.
 boolean isIgnoreWarnings()
          Return whether or not we ignore SQLWarnings.
protected  Map processResultSet(ResultSet rs, ResultSetSupportingSqlParameter param)
          Process the given ResultSet from a stored procedure.
protected  Object query(PreparedStatementCreator psc, PreparedStatementSetter pss, ResultSetExtractor rse)
          Query using a prepared statement, allowing for a PreparedStatementCreator and a PreparedStatementSetter.
 Object query(PreparedStatementCreator psc, ResultSetExtractor rse)
          Query using a prepared statement.
 List query(PreparedStatementCreator psc, RowCallbackHandler rch)
          Query using a prepared statement, reading the ResultSet on a per-row basis with a RowCallbackHandler (potentially implementing the ResultReader sub-interface that provides a result List).
 List query(PreparedStatementCreator psc, RowMapper rowMapper)
          Query using a prepared statement, mapping each row to a Java object via a RowMapper.
 Object query(String sql, Object[] args, int[] argTypes, ResultSetExtractor rse)
          Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, reading the ResultSet on a per-row basis with a RowCallbackHandler (potentially implementing the ResultReader sub-interface that provides a result List).
 List query(String sql, Object[] args, int[] argTypes, RowCallbackHandler rch)
          Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, reading the ResultSet on a per-row basis with a RowCallbackHandler (potentially implementing the ResultReader sub-interface that provides a result List).
 List query(String sql, Object[] args, int[] argTypes, RowMapper rowMapper)
          Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, mapping each row to a Java object via a RowMapper.
 Object query(String sql, Object[] args, ResultSetExtractor rse)
          Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, reading the ResultSet on a per-row basis with a RowCallbackHandler (potentially implementing the ResultReader sub-interface that provides a result List).
 List query(String sql, Object[] args, RowCallbackHandler rch)
          Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, reading the ResultSet on a per-row basis with a RowCallbackHandler (potentially implementing the ResultReader sub-interface that provides a result List).
 List query(String sql, Object[] args, RowMapper rowMapper)
          Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, mapping each row to a Java object via a RowMapper.
 Object query(String sql, PreparedStatementSetter pss, ResultSetExtractor rse)
          Query using a prepared statement, reading the ResultSet with a ResultSetExtractor.
 List query(String sql, PreparedStatementSetter pss, RowCallbackHandler rch)
          Query given SQL to create a prepared statement from SQL and a PreparedStatementSetter implementation that knows how to bind values to the query, reading the ResultSet on a per-row basis with a RowCallbackHandler (potentially implementing the ResultReader sub-interface that provides a result List).
 List query(String sql, PreparedStatementSetter pss, RowMapper rowMapper)
          Query given SQL to create a prepared statement from SQL and a PreparedStatementSetter implementation that knows how to bind values to the query, mapping each row to a Java objec via a RowMapper.
 Object query(String sql, ResultSetExtractor rse)
          Execute a query given static SQL, reading the ResultSet with a ResultSetExtractor.
 List query(String sql, RowCallbackHandler rch)
          Execute a query given static SQL, reading the ResultSet on a per-row basis with a RowCallbackHandler (potentially implementing the ResultReader sub-interface that provides a result List).
 List query(String sql, RowMapper rowMapper)
          Execute a query given static SQL, mapping each row to a Java object via a RowMapper.
 int queryForInt(String sql)
          Execute a query that results in an int value, given static SQL.
 int queryForInt(String sql, Object[] args)
          Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, resulting in an int value.
 int queryForInt(String sql, Object[] args, int[] argTypes)
          Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, resulting in an int value.
 List queryForList(String sql)
          Execute a query for a result list, given static SQL.
 List queryForList(String sql, Object[] args)
          Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, expecting a result list.
 List queryForList(String sql, Object[] args, int[] argTypes)
          Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, expecting a result list.
 long queryForLong(String sql)
          Execute a query that results in a long value, given static SQL.
 long queryForLong(String sql, Object[] args)
          Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, resulting in a long value.
 long queryForLong(String sql, Object[] args, int[] argTypes)
          Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, resulting in a long value.
 Object queryForObject(String sql, Class requiredType)
          Execute a query for a result object, given static SQL.
 Object queryForObject(String sql, Object[] args, Class requiredType)
          Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, expecting a result object.
 Object queryForObject(String sql, Object[] args, int[] argTypes, Class requiredType)
          Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, expecting a result object.
 void setFetchSize(int fetchSize)
          Set the fetch size for this JdbcTemplate.
 void setIgnoreWarnings(boolean ignoreWarnings)
          Set whether or not we want to ignore SQLWarnings.
 void setMaxRows(int maxRows)
          Set the maximum number of rows for this JdbcTemplate.
 void setNativeJdbcExtractor(NativeJdbcExtractor extractor)
          Set a NativeJdbcExtractor to extract native JDBC objects from wrapped handles.
 int update(PreparedStatementCreator psc)
          Issue an update using a PreparedStatementCreator to provide SQL and any required parameters.
 int update(PreparedStatementCreator psc, KeyHolder generatedKeyHolder)
          Issue an update using a PreparedStatementCreator to provide SQL and any required parameters.
protected  int update(PreparedStatementCreator psc, PreparedStatementSetter pss)
           
 int update(String sql)
          Issue a single SQL update.
 int update(String sql, Object[] args)
          Issue an update via a prepared statement, binding the given arguments.
 int update(String sql, Object[] args, int[] argTypes)
          Issue an update via a prepared statement, binding the given arguments.
 int update(String sql, PreparedStatementSetter pss)
          Issue an update using a PreparedStatementSetter to set bind parameters, with given SQL.
 
Methods inherited from class org.springframework.jdbc.support.JdbcAccessor
getDataSource, getExceptionTranslator, isLazyInit, setDataSource, setExceptionTranslator, setLazyInit
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JdbcTemplate

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:
JdbcAccessor.setDataSource(javax.sql.DataSource)

JdbcTemplate

public JdbcTemplate(DataSource dataSource)
Construct a new JdbcTemplate, given a DataSource to obtain connections from. Note: This will trigger eager initialization of the exception translator.

Parameters:
dataSource - JDBC DataSource to obtain connections from
Method Detail

setNativeJdbcExtractor

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).


getNativeJdbcExtractor

public NativeJdbcExtractor getNativeJdbcExtractor()
Return the current NativeJdbcExtractor implementation.


setIgnoreWarnings

public void setIgnoreWarnings(boolean ignoreWarnings)
Set whether or not we want to ignore SQLWarnings. Default is true.


isIgnoreWarnings

public boolean isIgnoreWarnings()
Return whether or not we ignore SQLWarnings. Default is true.


getIgnoreWarnings

public boolean getIgnoreWarnings()
Deprecated. in favor of isIgnoreWarnings

Return whether or not we ignore SQLWarnings. Default is true.

See Also:
isIgnoreWarnings()

setFetchSize

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 driver's default.


getFetchSize

public int getFetchSize()
Return the fetch size specified for this JdbcTemplate.


setMaxRows

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.

Default is 0, indicating to use the driver's default.


getMaxRows

public int getMaxRows()
Return the maximum number of rows specified for this JdbcTemplate.


afterPropertiesSet

public void afterPropertiesSet()
Description copied from interface: InitializingBean
Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware).

This method allows the bean instance to perform initialization only possible when all bean properties have been set and to throw an exception in the event of misconfiguration.

Specified by:
afterPropertiesSet in interface InitializingBean
Overrides:
afterPropertiesSet in class JdbcAccessor

execute

public Object execute(ConnectionCallback action)
               throws DataAccessException
Description copied from interface: JdbcOperations
Execute the action specified by the given action object within a JDBC Connection. Allows for returning a result object, i.e. a domain object or a collection of domain objects.

Specified by:
execute in interface JdbcOperations
Parameters:
action - callback object that specifies the action
Returns:
a result object returned by the action, or null
Throws:
DataAccessException - if there is any problem

execute

public Object execute(StatementCallback action)
               throws DataAccessException
Description copied from interface: JdbcOperations
Execute the action specified by the given action object within a JDBC Statement. Allows for returning a result object, i.e. a domain object or a collection of domain objects.

Specified by:
execute in interface JdbcOperations
Parameters:
action - callback object that specifies the action
Returns:
a result object returned by the action, or null
Throws:
DataAccessException - if there is any problem

execute

public void execute(String sql)
             throws DataAccessException
Description copied from interface: JdbcOperations
Issue a single SQL execute, typically a DDL statement.

Specified by:
execute in interface JdbcOperations
Parameters:
sql - static SQL to execute
Throws:
DataAccessException - if there is any problem

query

public Object query(String sql,
                    ResultSetExtractor rse)
             throws DataAccessException
Description copied from interface: JdbcOperations
Execute a query given static SQL, reading the ResultSet with a ResultSetExtractor.

Uses a JDBC Statement, not a PreparedStatement. If you want to execute a static query with a PreparedStatement, use the overloaded query method with a null PreparedStatementSetter as a parameter.

Specified by:
query in interface JdbcOperations
Parameters:
sql - SQL query to execute
rse - object that will extract all rows of results
Returns:
an arbitrary result object, as returned by the ResultSetExtractor
Throws:
DataAccessException - if there is any problem executing the query
See Also:
JdbcOperations.query(String, PreparedStatementSetter, ResultSetExtractor)

query

public List query(String sql,
                  RowCallbackHandler rch)
           throws DataAccessException
Description copied from interface: JdbcOperations
Execute a query given static SQL, reading the ResultSet on a per-row basis with a RowCallbackHandler (potentially implementing the ResultReader sub-interface that provides a result List).

Uses a JDBC Statement, not a PreparedStatement. If you want to execute a static query with a PreparedStatement, use the overloaded query method with null as PreparedStatementSetter argument.

Specified by:
query in interface JdbcOperations
Parameters:
sql - SQL query to execute
rch - object that will extract results (potentially a ResultReader), one row at a time
Returns:
the result List in case of a ResultReader, or null else
Throws:
DataAccessException - if there is any problem executing the query
See Also:
JdbcOperations.query(String, PreparedStatementSetter, RowCallbackHandler)

query

public List query(String sql,
                  RowMapper rowMapper)
           throws DataAccessException
Description copied from interface: JdbcOperations
Execute a query given static SQL, mapping each row to a Java object via a RowMapper.

Uses a JDBC Statement, not a PreparedStatement. If you want to execute a static query with a PreparedStatement, use the overloaded query method with null as PreparedStatementSetter argument.

Specified by:
query in interface JdbcOperations
Parameters:
sql - SQL query to execute
rowMapper - object that will map one object per row
Returns:
the result List in case of a ResultReader, or null else
Throws:
DataAccessException - if there is any problem executing the query
See Also:
JdbcOperations.query(String, PreparedStatementSetter, RowCallbackHandler)

queryForList

public List queryForList(String sql)
                  throws DataAccessException
Description copied from interface: JdbcOperations
Execute a query for a result list, given static SQL.

Uses a JDBC Statement, not a PreparedStatement. If you want to execute a static query with a PreparedStatement, use the overloaded queryForList method with null as argument array.

This method is useful for running static SQL with a known outcome. The results will be mapped to an ArrayList (one entry for each row) of HashMaps (one entry for each column using the column name as the key).

Specified by:
queryForList in interface JdbcOperations
Parameters:
sql - SQL query to execute
Returns:
an ArrayList that contains a HashMap per row
Throws:
DataAccessException - if there is any problem executing the query
See Also:
JdbcOperations.queryForList(String, Object[])

queryForObject

public Object queryForObject(String sql,
                             Class requiredType)
                      throws DataAccessException
Description copied from interface: JdbcOperations
Execute a query for a result object, given static SQL.

Uses a JDBC Statement, not a PreparedStatement. If you want to execute a static query with a PreparedStatement, use the overloaded queryForObject method with null as argument array.

This method is useful for running static SQL with a known outcome. The query is expected to be a single row/single column query; the returned result will be directly mapped to the corresponding object type.

Specified by:
queryForObject in interface JdbcOperations
Parameters:
sql - SQL query to execute
requiredType - the type that the result object is expected to match
Returns:
the result object of the required type, or null in case of SQL NULL
Throws:
DataAccessException - if there is any problem executing the query
See Also:
JdbcOperations.queryForObject(String, Object[], Class)

queryForLong

public long queryForLong(String sql)
                  throws DataAccessException
Description copied from interface: JdbcOperations
Execute a query that results in a long value, given static SQL.

Uses a JDBC Statement, not a PreparedStatement. If you want to execute a static query with a PreparedStatement, use the overloaded queryForLong method with null as argument array.

This method is useful for running static SQL with a known outcome. The query is expected to be a single row/single column query that results in a long value.

Specified by:
queryForLong in interface JdbcOperations
Parameters:
sql - SQL query to execute
Returns:
the long value, or 0 in case of SQL NULL
Throws:
DataAccessException - if there is any problem executing the query
See Also:
JdbcOperations.queryForLong(String, Object[])

queryForInt

public int queryForInt(String sql)
                throws DataAccessException
Description copied from interface: JdbcOperations
Execute a query that results in an int value, given static SQL.

Uses a JDBC Statement, not a PreparedStatement. If you want to execute a static query with a PreparedStatement, use the overloaded queryForInt method with null as argument array.

This method is useful for running static SQL with a known outcome. The query is expected to be a single row/single column query that results in an int value.

Specified by:
queryForInt in interface JdbcOperations
Parameters:
sql - SQL query to execute
Returns:
the int value, or 0 in case of SQL NULL
Throws:
DataAccessException - if there is any problem executing the query
See Also:
JdbcOperations.queryForInt(String, Object[])

update

public int update(String sql)
           throws DataAccessException
Description copied from interface: JdbcOperations
Issue a single SQL update.

Specified by:
update in interface JdbcOperations
Parameters:
sql - static SQL to execute
Returns:
the number of rows affected
Throws:
DataAccessException - if there is any problem.

batchUpdate

public int[] batchUpdate(String[] sql)
                  throws DataAccessException
Description copied from interface: JdbcOperations
Issue multiple SQL updates on a single Statement, using JDBC 2.0 batching.

Will fall back to separate updates on a single Statement if the JDBC driver does not support batch updates.

Specified by:
batchUpdate in interface JdbcOperations
Parameters:
sql - defining an array of SQL statements that will be executed.
Returns:
an array of the number of rows affected by each statement
Throws:
DataAccessException - if there is any problem executing the batch

execute

public Object execute(PreparedStatementCreator psc,
                      PreparedStatementCallback action)
               throws DataAccessException
Description copied from interface: JdbcOperations
Execute the action specified by the given action object within a JDBC PreparedStatement. Allows for returning a result object, i.e. a domain object or a collection of domain objects.

Specified by:
execute in interface JdbcOperations
Parameters:
psc - object that can create a PreparedStatement given a Connection
action - callback object that specifies the action
Returns:
a result object returned by the action, or null
Throws:
DataAccessException - if there is any problem

execute

public Object execute(String sql,
                      PreparedStatementCallback action)
               throws DataAccessException
Description copied from interface: JdbcOperations
Execute the action specified by the given action object within a JDBC PreparedStatement. Allows for returning a result object, i.e. a domain object or a collection of domain objects.

Specified by:
execute in interface JdbcOperations
Parameters:
sql - SQL to execute
action - callback object that specifies the action
Returns:
a result object returned by the action, or null
Throws:
DataAccessException - if there is any problem

query

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 Connection
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.
rse - object that will extract results.
Returns:
an arbitrary result object, as returned by the ResultSetExtractor
Throws:
DataAccessException - if there is any problem

query

public Object query(PreparedStatementCreator psc,
                    ResultSetExtractor rse)
             throws DataAccessException
Description copied from interface: JdbcOperations
Query using a prepared statement.

Specified by:
query in interface JdbcOperations
Parameters:
psc - object that can create a PreparedStatement given a Connection
rse - object that will extract results
Returns:
an arbitrary result object, as returned by the ResultSetExtractor
Throws:
DataAccessException - if there is any problem

query

public Object query(String sql,
                    PreparedStatementSetter pss,
                    ResultSetExtractor rse)
             throws DataAccessException
Description copied from interface: JdbcOperations
Query using a prepared statement, reading the ResultSet with a ResultSetExtractor.

Specified by:
query in interface JdbcOperations
Parameters:
sql - SQL to execute
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. Even if there are no bind parameters, this object may be used to set fetch size and other performance options.
rse - object that will extract results
Returns:
an arbitrary result object, as returned by the ResultSetExtractor
Throws:
DataAccessException - if there is any problem

query

public Object query(String sql,
                    Object[] args,
                    int[] argTypes,
                    ResultSetExtractor rse)
             throws DataAccessException
Description copied from interface: JdbcOperations
Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, reading the ResultSet on a per-row basis with a RowCallbackHandler (potentially implementing the ResultReader sub-interface that provides a result List).

Specified by:
query in interface JdbcOperations
Parameters:
sql - SQL to execute
args - arguments to bind to the query
argTypes - SQL types of the arguments (constants from java.sql.Types)
rse - object that will extract results
Returns:
the result List in case of a ResultReader, or null else
Throws:
DataAccessException - if the query fails
See Also:
Types

query

public Object query(String sql,
                    Object[] args,
                    ResultSetExtractor rse)
             throws DataAccessException
Description copied from interface: JdbcOperations
Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, reading the ResultSet on a per-row basis with a RowCallbackHandler (potentially implementing the ResultReader sub-interface that provides a result List).

Specified by:
query in interface JdbcOperations
Parameters:
sql - SQL to execute
args - arguments to bind to the query (leaving it to the PreparedStatement to guess the respective SQL type)
rse - object that will extract results
Returns:
the result List in case of a ResultReader, or null else
Throws:
DataAccessException - if the query fails

query

public List query(PreparedStatementCreator psc,
                  RowCallbackHandler rch)
           throws DataAccessException
Description copied from interface: JdbcOperations
Query using a prepared statement, reading the ResultSet on a per-row basis with a RowCallbackHandler (potentially implementing the ResultReader sub-interface that provides a result List).

Specified by:
query in interface JdbcOperations
Parameters:
psc - object that can create a PreparedStatement given a Connection
rch - object that will extract results (potentially a ResultReader), one row at a time
Returns:
the result List in case of a ResultReader, or null else
Throws:
DataAccessException - if there is any problem

query

public List query(String sql,
                  PreparedStatementSetter pss,
                  RowCallbackHandler rch)
           throws DataAccessException
Description copied from interface: JdbcOperations
Query given SQL to create a prepared statement from SQL and a PreparedStatementSetter implementation that knows how to bind values to the query, reading the ResultSet on a per-row basis with a RowCallbackHandler (potentially implementing the ResultReader sub-interface that provides a result List).

Specified by:
query in interface JdbcOperations
Parameters:
sql - SQL to execute
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. Even if there are no bind parameters, this object may be used to set fetch size and other performance options.
rch - object that will extract results (potentially a ResultReader), one row at a time
Returns:
the result List in case of a ResultReader, or null else
Throws:
DataAccessException - if the query fails

query

public List query(String sql,
                  Object[] args,
                  int[] argTypes,
                  RowCallbackHandler rch)
           throws DataAccessException
Description copied from interface: JdbcOperations
Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, reading the ResultSet on a per-row basis with a RowCallbackHandler (potentially implementing the ResultReader sub-interface that provides a result List).

Specified by:
query in interface JdbcOperations
Parameters:
sql - SQL to execute
args - arguments to bind to the query
argTypes - SQL types of the arguments (constants from java.sql.Types)
rch - object that will extract results (potentially a ResultReader), one row at a time
Returns:
the result List in case of a ResultReader, or null else
Throws:
DataAccessException - if the query fails
See Also:
Types

query

public List query(String sql,
                  Object[] args,
                  RowCallbackHandler rch)
           throws DataAccessException
Description copied from interface: JdbcOperations
Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, reading the ResultSet on a per-row basis with a RowCallbackHandler (potentially implementing the ResultReader sub-interface that provides a result List).

Specified by:
query in interface JdbcOperations
Parameters:
sql - SQL to execute
args - arguments to bind to the query (leaving it to the PreparedStatement to guess the respective SQL type)
rch - object that will extract results (potentially a ResultReader), one row at a time
Returns:
the result List in case of a ResultReader, or null else
Throws:
DataAccessException - if the query fails

query

public List query(PreparedStatementCreator psc,
                  RowMapper rowMapper)
           throws DataAccessException
Description copied from interface: JdbcOperations
Query using a prepared statement, mapping each row to a Java object via a RowMapper.

Specified by:
query in interface JdbcOperations
Parameters:
psc - object that can create a PreparedStatement given a Connection
rowMapper - object that will map one object per row
Returns:
the result List in case of a ResultReader, or null else
Throws:
DataAccessException - if there is any problem

query

public List query(String sql,
                  PreparedStatementSetter pss,
                  RowMapper rowMapper)
           throws DataAccessException
Description copied from interface: JdbcOperations
Query given SQL to create a prepared statement from SQL and a PreparedStatementSetter implementation that knows how to bind values to the query, mapping each row to a Java objec via a RowMapper.

Specified by:
query in interface JdbcOperations
Parameters:
sql - SQL to execute
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. Even if there are no bind parameters, this object may be used to set fetch size and other performance options.
rowMapper - object that will map one object per row
Returns:
the result List in case of a ResultReader, or null else
Throws:
DataAccessException - if the query fails

query

public List query(String sql,
                  Object[] args,
                  int[] argTypes,
                  RowMapper rowMapper)
           throws DataAccessException
Description copied from interface: JdbcOperations
Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, mapping each row to a Java object via a RowMapper.

Specified by:
query in interface JdbcOperations
Parameters:
sql - SQL to execute
args - arguments to bind to the query
argTypes - SQL types of the arguments (constants from java.sql.Types)
rowMapper - object that will map one object per row
Returns:
the result List in case of a ResultReader, or null else
Throws:
DataAccessException - if the query fails
See Also:
Types

query

public List query(String sql,
                  Object[] args,
                  RowMapper rowMapper)
           throws DataAccessException
Description copied from interface: JdbcOperations
Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, mapping each row to a Java object via a RowMapper.

Specified by:
query in interface JdbcOperations
Parameters:
sql - SQL to execute
args - arguments to bind to the query (leaving it to the PreparedStatement to guess the respective SQL type)
rowMapper - object that will map one object per row
Returns:
the result List in case of a ResultReader, or null else
Throws:
DataAccessException - if the query fails

queryForList

public List queryForList(String sql,
                         Object[] args,
                         int[] argTypes)
                  throws DataAccessException
Description copied from interface: JdbcOperations
Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, expecting a result list.

This method is useful for running static SQL with a known outcome. The results will be mapped to an ArrayList (one entry for each row) of HashMaps (one entry for each column using the column name as the key).

Specified by:
queryForList in interface JdbcOperations
Parameters:
sql - SQL to execute
args - arguments to bind to the query (leaving it to the PreparedStatement to guess the respective SQL type)
argTypes - SQL types of the arguments (constants from java.sql.Types)
Returns:
an ArrayList that contains a HashMap per row
Throws:
DataAccessException - if the query fails
See Also:
JdbcOperations.queryForList(String), Types

queryForList

public List queryForList(String sql,
                         Object[] args)
                  throws DataAccessException
Description copied from interface: JdbcOperations
Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, expecting a result list.

This method is useful for running static SQL with a known outcome. The results will be mapped to an ArrayList (one entry for each row) of HashMaps (one entry for each column using the column name as the key).

Specified by:
queryForList in interface JdbcOperations
Parameters:
sql - SQL to execute
args - arguments to bind to the query (leaving it to the PreparedStatement to guess the respective SQL type)
Returns:
an ArrayList that contains a HashMap per row
Throws:
DataAccessException - if the query fails
See Also:
JdbcOperations.queryForList(String)

queryForObject

public Object queryForObject(String sql,
                             Object[] args,
                             int[] argTypes,
                             Class requiredType)
                      throws DataAccessException
Description copied from interface: JdbcOperations
Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, expecting a result object.

This method is useful for running static SQL with a known outcome. The query is expected to be a single row/single column query; the returned result will be directly mapped to the corresponding object type.

Specified by:
queryForObject in interface JdbcOperations
Parameters:
sql - SQL to execute
args - arguments to bind to the query (leaving it to the PreparedStatement to guess the respective SQL type)
argTypes - SQL types of the arguments (constants from java.sql.Types)
requiredType - the type that the result object is expected to match
Returns:
the result object of the required type, or null in case of SQL NULL
Throws:
DataAccessException - if the query fails
See Also:
JdbcOperations.queryForObject(String, Class), Types

queryForObject

public Object queryForObject(String sql,
                             Object[] args,
                             Class requiredType)
                      throws DataAccessException
Description copied from interface: JdbcOperations
Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, expecting a result object.

This method is useful for running static SQL with a known outcome. The query is expected to be a single row/single column query; the returned result will be directly mapped to the corresponding object type.

Specified by:
queryForObject in interface JdbcOperations
Parameters:
sql - SQL to execute
args - arguments to bind to the query (leaving it to the PreparedStatement to guess the respective SQL type)
requiredType - the type that the result object is expected to match
Returns:
the result object of the required type, or null in case of SQL NULL
Throws:
DataAccessException - if the query fails
See Also:
JdbcOperations.queryForObject(String, Class)

queryForLong

public long queryForLong(String sql,
                         Object[] args,
                         int[] argTypes)
                  throws DataAccessException
Description copied from interface: JdbcOperations
Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, resulting in a long value.

This method is useful for running static SQL with a known outcome. The query is expected to be a single row/single column query that results in a long value.

Specified by:
queryForLong in interface JdbcOperations
Parameters:
sql - SQL to execute
args - arguments to bind to the query (leaving it to the PreparedStatement to guess the respective SQL type)
argTypes - SQL types of the arguments (constants from java.sql.Types)
Returns:
the long value, or 0 in case of SQL NULL
Throws:
DataAccessException - if the query fails
See Also:
JdbcOperations.queryForLong(String), Types

queryForLong

public long queryForLong(String sql,
                         Object[] args)
                  throws DataAccessException
Description copied from interface: JdbcOperations
Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, resulting in a long value.

This method is useful for running static SQL with a known outcome. The query is expected to be a single row/single column query that results in a long value.

Specified by:
queryForLong in interface JdbcOperations
Parameters:
sql - SQL to execute
args - arguments to bind to the query (leaving it to the PreparedStatement to guess the respective SQL type)
Returns:
the long value, or 0 in case of SQL NULL
Throws:
DataAccessException - if the query fails
See Also:
JdbcOperations.queryForLong(String)

queryForInt

public int queryForInt(String sql,
                       Object[] args,
                       int[] argTypes)
                throws DataAccessException
Description copied from interface: JdbcOperations
Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, resulting in an int value.

This method is useful for running static SQL with a known outcome. The query is expected to be a single row/single column query that results in an int value.

Specified by:
queryForInt in interface JdbcOperations
Parameters:
sql - SQL to execute
args - arguments to bind to the query (leaving it to the PreparedStatement to guess the respective SQL type)
argTypes - SQL types of the arguments (constants from java.sql.Types)
Returns:
the int value, or 0 in case of SQL NULL
Throws:
DataAccessException - if the query fails
See Also:
JdbcOperations.queryForInt(String), Types

queryForInt

public int queryForInt(String sql,
                       Object[] args)
                throws DataAccessException
Description copied from interface: JdbcOperations
Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, resulting in an int value.

This method is useful for running static SQL with a known outcome. The query is expected to be a single row/single column query that results in an int value.

Specified by:
queryForInt in interface JdbcOperations
Parameters:
sql - SQL to execute
args - arguments to bind to the query (leaving it to the PreparedStatement to guess the respective SQL type)
Returns:
the int value, or 0 in case of SQL NULL
Throws:
DataAccessException - if the query fails
See Also:
JdbcOperations.queryForInt(String)

update

protected int update(PreparedStatementCreator psc,
                     PreparedStatementSetter pss)
              throws DataAccessException
Throws:
DataAccessException

update

public int update(PreparedStatementCreator psc)
           throws DataAccessException
Description copied from interface: JdbcOperations
Issue an update using a PreparedStatementCreator to provide SQL and any required parameters.

Specified by:
update in interface JdbcOperations
Parameters:
psc - object that provides SQL and any necessary parameters
Returns:
the number of rows affected
Throws:
DataAccessException - if there is any problem issuing the update

update

public int update(PreparedStatementCreator psc,
                  KeyHolder generatedKeyHolder)
           throws DataAccessException
Description copied from interface: JdbcOperations
Issue an update using a PreparedStatementCreator to provide SQL and any required parameters. Generetaed keys will to be returned by the List parameter.

Specified by:
update in interface JdbcOperations
Parameters:
psc - object that provides SQL and any necessary parameters
generatedKeyHolder - KeyHolder that will hold the generated keys
Returns:
the number of rows affected
Throws:
DataAccessException - if there is any problem issuing the update

update

public int update(String sql,
                  PreparedStatementSetter pss)
           throws DataAccessException
Description copied from interface: JdbcOperations
Issue an update using a PreparedStatementSetter to set bind parameters, with given SQL. Simpler than using a PreparedStatementCreator as this method will create the PreparedStatement: The PreparedStatementSetter just needs to set parameters.

Specified by:
update in interface JdbcOperations
Parameters:
sql - SQL, containing bind parameters
pss - helper that sets bind parameters. If this is null we run an update with static SQL.
Returns:
the number of rows affected
Throws:
DataAccessException - if there is any problem issuing the update

update

public int update(String sql,
                  Object[] args,
                  int[] argTypes)
           throws DataAccessException
Description copied from interface: JdbcOperations
Issue an update via a prepared statement, binding the given arguments.

Specified by:
update in interface JdbcOperations
Parameters:
sql - SQL, containing bind parameters
args - arguments to bind to the query
argTypes - SQL types of the arguments (constants from java.sql.Types)
Returns:
the number of rows affected
Throws:
DataAccessException - if there is any problem issuing the update
See Also:
Types

update

public int update(String sql,
                  Object[] args)
           throws DataAccessException
Description copied from interface: JdbcOperations
Issue an update via a prepared statement, binding the given arguments.

Specified by:
update in interface JdbcOperations
Parameters:
sql - SQL, containing bind parameters
args - arguments to bind to the query (leaving it to the PreparedStatement to guess the respective SQL type)
Returns:
the number of rows affected
Throws:
DataAccessException - if there is any problem issuing the update

batchUpdate

public int[] batchUpdate(String sql,
                         BatchPreparedStatementSetter pss)
                  throws DataAccessException
Description copied from interface: JdbcOperations
Issue multiple updates on a single PreparedStatement, using JDBC 2.0 batch updates and a BatchPreparedStatementSetter to set values.

Will fall back to separate updates on a single PreparedStatement if the JDBC driver does not support batch updates.

Specified by:
batchUpdate in interface JdbcOperations
Parameters:
sql - defining PreparedStatement that will be reused. All statements in the batch will use the same SQL.
pss - object to set parameters on the PreparedStatement created by this method
Returns:
an array of the number of rows affected by each statement
Throws:
DataAccessException - if there is any problem issuing the update

execute

public Object execute(CallableStatementCreator csc,
                      CallableStatementCallback action)
               throws DataAccessException
Description copied from interface: JdbcOperations
Execute the action specified by the given action object within a JDBC CallableStatement. Allows for returning a result object, i.e. a domain object or a collection of domain objects.

Specified by:
execute in interface JdbcOperations
Parameters:
csc - object that can create a CallableStatement given a Connection
action - callback object that specifies the action
Returns:
a result object returned by the action, or null
Throws:
DataAccessException - if there is any problem

execute

public Object execute(String callString,
                      CallableStatementCallback action)
               throws DataAccessException
Description copied from interface: JdbcOperations
Execute the action specified by the given action object within a JDBC CallableStatement. Allows for returning a result object, i.e. a domain object or a collection of domain objects.

Specified by:
execute in interface JdbcOperations
Parameters:
callString - the SQL call string to execute
action - callback object that specifies the action
Returns:
a result object returned by the action, or null
Throws:
DataAccessException - if there is any problem

call

public Map call(CallableStatementCreator csc,
                List declaredParameters)
         throws DataAccessException
Description copied from interface: JdbcOperations
Execute a SQL call using a CallableStatementCreator to provide SQL and any required parameters.

Specified by:
call in interface JdbcOperations
Parameters:
csc - object that provides SQL and any necessary parameters
declaredParameters - list of declared SqlParameter objects
Returns:
Map of extracted out parameters
Throws:
DataAccessException - if there is any problem issuing the update

extractReturnedResultSets

protected Map extractReturnedResultSets(CallableStatement cs,
                                        List parameters,
                                        int updateCount)
                                 throws SQLException
Extract returned ResultSets from the completed stored procedure.

Parameters:
cs - JDBC wrapper for the stored procedure
parameters - Parameter list for the stored procedure
Returns:
Map that contains returned results
Throws:
SQLException

extractOutputParameters

protected Map extractOutputParameters(CallableStatement cs,
                                      List parameters)
                               throws SQLException
Extract output parameters from the completed stored procedure.

Parameters:
cs - JDBC wrapper for the stored procedure
parameters - parameter list for the stored procedure
Returns:
parameters to the stored procedure
Throws:
SQLException

processResultSet

protected Map processResultSet(ResultSet rs,
                               ResultSetSupportingSqlParameter param)
                        throws SQLException
Process the given ResultSet from a stored procedure.

Parameters:
rs - the ResultSet to process
param - the corresponding stored procedure parameter
Returns:
Map that contains returned results
Throws:
SQLException


Copyright (C) 2003-2004 The Spring Framework Project.