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 SQLExceptionTranslator
interface, there should be no need to subclass it.

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
A PreparedStatementCreator can either be implemented directly or
configured through a PreparedStatementCreatorFactory.

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 with a
ResultSetExtractor.

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

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.

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 with a
ResultSetExtractor.

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

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.

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

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 object via a RowMapper.

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

queryForList(String sql,
Object[] args,
int[] argTypes,
Class elementType)
Query given SQL to create a prepared statement from SQL and a
list of arguments to bind to the query, expecting a result list.

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.

queryForObject(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 a single result row to a
Java object via a RowMapper.

queryForObject(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 a single result row to a
Java object via a RowMapper.

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.

setNativeJdbcExtractor

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

isIgnoreWarnings

public boolean isIgnoreWarnings()

Return whether or not we ignore SQLWarnings.

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.

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

execute

Execute a JDBC data access operation, implemented as callback action
working on a JDBC Statement. This allows for implementing arbitrary data
access operations on a single Statement, within Spring's managed JDBC
environment: that is, participating in Spring-managed transactions and
converting JDBC SQLExceptions into Spring's DataAccessException hierarchy.

The callback action can return a result object, for example a
domain object or a collection of domain objects.

queryForObject

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.

queryForList

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.

The results will be mapped to a List (one entry for each row) of
Maps (one entry for each column using the column name as the key).
Each element in the list will be of the form returned by this interface's
queryForMap() methods.

queryForRowSet

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

The results will be mapped to an SqlRowSet which holds the data in a
disconnected fashion. This wrapper will translate any SQLExceptions thrown.

Note that that, for the default implementation, JDBC RowSet support needs to
be available at runtime: by default, Sun's com.sun.rowset.CachedRowSetImpl
class is used, which is part of JDK 1.5+ and also available separately as part of
Sun's JDBC RowSet Implementations download (rowset.jar).

execute

Execute a JDBC data access operation, implemented as callback action
working on a JDBC PreparedStatement. This allows for implementing arbitrary
data access operations on a single Statement, within Spring's managed
JDBC environment: that is, participating in Spring-managed transactions
and converting JDBC SQLExceptions into Spring's DataAccessException hierarchy.

The callback action can return a result object, for example a
domain object or a collection of domain objects.

execute

Execute a JDBC data access operation, implemented as callback action
working on a JDBC PreparedStatement. This allows for implementing arbitrary
data access operations on a single Statement, within Spring's managed
JDBC environment: that is, participating in Spring-managed transactions
and converting JDBC SQLExceptions into Spring's DataAccessException hierarchy.

The callback action can return a result object, for example a
domain object or a collection of domain objects.

query

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.

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.

query

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

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

query

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

query

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

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.

queryForMap

Query given SQL to create a prepared statement from SQL and a
list of arguments to bind to the query, expecting a result Map.
The queryForMap() methods defined by this interface are appropriate
when you don't have a domain model. Otherwise, consider using
one of the queryForObject() methods.

The query is expected to be a single row query; the result row will be
mapped to a Map (one entry for each column, using the column name as the key).

queryForList

Query given SQL to create a prepared statement from SQL and a
list of arguments to bind to the query, expecting a result list.

The results will be mapped to a List (one entry for each row) of
Maps (one entry for each column, using the column name as the key).
Thus Each element in the list will be of the form returned by this interface's
queryForMap() methods.

queryForList

Query given SQL to create a prepared statement from SQL and a
list of arguments to bind to the query, expecting a result list.

The results will be mapped to a List (one entry for each row) of
Maps (one entry for each column, using the column name as the key).
Each element in the list will be of the form returned by this interface's
queryForMap() methods.

queryForRowSet

Query given SQL to create a prepared statement from SQL and a
list of arguments to bind to the query, expecting a SqlRowSet.

The results will be mapped to an SqlRowSet which holds the data in a
disconnected fashion. This wrapper will translate any SQLExceptions thrown.

Note that that, for the default implementation, JDBC RowSet support needs to
be available at runtime: by default, Sun's com.sun.rowset.CachedRowSetImpl
class is used, which is part of JDK 1.5+ and also available separately as part of
Sun's JDBC RowSet Implementations download (rowset.jar).

queryForRowSet

Query given SQL to create a prepared statement from SQL and a
list of arguments to bind to the query, expecting a SqlRowSet.

The results will be mapped to an SqlRowSet which holds the data in a
disconnected fashion. This wrapper will translate any SQLExceptions thrown.

Note that that, for the default implementation, JDBC RowSet support needs to
be available at runtime: by default, Sun's com.sun.rowset.CachedRowSetImpl
class is used, which is part of JDK 1.5+ and also available separately as part of
Sun's JDBC RowSet Implementations download (rowset.jar).

update

Issue an update using a PreparedStatementCreator to provide SQL and any
required parameters. Generated keys will be put into the given KeyHolder.

Note that the given PreparedStatementCreator has to create a statement
with activated extraction of generated keys (a JDBC 3.0 feature). This can
either be done directly or through using a PreparedStatementCreatorFactory.

update

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.

execute

Execute a JDBC data access operation, implemented as callback action
working on a JDBC CallableStatement. This allows for implementing arbitrary
data access operations on a single Statement, within Spring's managed
JDBC environment: that is, participating in Spring-managed transactions
and converting JDBC SQLExceptions into Spring's DataAccessException hierarchy.

The callback action can return a result object, for example a
domain object or a collection of domain objects.

execute

Execute a JDBC data access operation, implemented as callback action
working on a JDBC CallableStatement. This allows for implementing arbitrary
data access operations on a single Statement, within Spring's managed
JDBC environment: that is, participating in Spring-managed transactions
and converting JDBC SQLExceptions into Spring's DataAccessException hierarchy.

The callback action can return a result object, for example a
domain object or a collection of domain objects.