The Spring Framework

org.springframework.jdbc.core.simple
Class SimpleJdbcTemplate

java.lang.Object
  extended by org.springframework.jdbc.core.simple.SimpleJdbcTemplate
All Implemented Interfaces:
SimpleJdbcOperations

public class SimpleJdbcTemplate
extends Object
implements SimpleJdbcOperations

Java-5-based convenience wrapper for the classic Spring JdbcTemplate, taking advantage of varargs and autoboxing, and exposing only the most commonly required operations in order to simplify JdbcTemplate usage.

Use the getJdbcOperations() method (or a straight JdbcTemplate) if you need to invoke less commonly used template methods. This includes any methods specifying SQL types, methods using less commonly used callbacks such as RowCallbackHandler, updates with PreparedStatementSetters rather than argument arrays, and stored procedures as well as batch operations.

Since:
2.0
Author:
Rod Johnson, Rob Harrop, Juergen Hoeller
See Also:
ParameterizedRowMapper, SimpleJdbcDaoSupport, JdbcTemplate

Constructor Summary
SimpleJdbcTemplate(DataSource dataSource)
          Create a new SimpleJdbcTemplate for the given DataSource.
SimpleJdbcTemplate(JdbcOperations classicJdbcTemplate)
          Create a new SimpleJdbcTemplate for the given classic Spring JdbcTemplate.
 
Method Summary
 JdbcOperations getJdbcOperations()
          Expose the classic Spring JdbcTemplate to allow invocation of less commonly used methods.
<T> List<T>
query(String sql, ParameterizedRowMapper<T> rm, Object... args)
          Query for a List of Objects of type T using the supplied ParameterizedRowMapper to the query results to the object.
 int queryForInt(String sql, Object... args)
          Query for an int passing in a SQL query and a variable number of arguments.
 List<Map<String,Object>> queryForList(String sql, Object... args)
          Execute the supplied query with the (optional) supplied arguments.
 long queryForLong(String sql, Object... args)
          Query for an long passing in a SQL query and a variable number of arguments.
 Map<String,Object> queryForMap(String sql, Object... args)
          Execute the supplied query with the (optional) supplied arguments.
<T> T
queryForObject(String sql, Class<T> requiredType, Object... args)
          Query for an object of type T identified by the supplied @Class.
<T> T
queryForObject(String sql, ParameterizedRowMapper<T> rm, Object... args)
          Query for an object of type T using the supplied ParameterizedRowMapper to the query results to the object.
 int update(String sql, Object... args)
          Execute the supplied SQL statement with (optional) supplied arguments.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SimpleJdbcTemplate

public SimpleJdbcTemplate(DataSource dataSource)
Create a new SimpleJdbcTemplate for the given DataSource.

Creates a classic Spring JdbcTemplate and wraps it.

Parameters:
dataSource - the JDBC DataSource to access

SimpleJdbcTemplate

public SimpleJdbcTemplate(JdbcOperations classicJdbcTemplate)
Create a new SimpleJdbcTemplate for the given classic Spring JdbcTemplate.

Parameters:
classicJdbcTemplate - the classic Spring JdbcTemplate to wrap
Method Detail

getJdbcOperations

public JdbcOperations getJdbcOperations()
Expose the classic Spring JdbcTemplate to allow invocation of less commonly used methods.

Specified by:
getJdbcOperations in interface SimpleJdbcOperations

queryForInt

public int queryForInt(String sql,
                       Object... args)
                throws DataAccessException
Description copied from interface: SimpleJdbcOperations
Query for an int passing in a SQL query and a variable number of arguments.

Specified by:
queryForInt in interface SimpleJdbcOperations
Throws:
DataAccessException

queryForLong

public long queryForLong(String sql,
                         Object... args)
                  throws DataAccessException
Description copied from interface: SimpleJdbcOperations
Query for an long passing in a SQL query and a variable number of arguments.

Specified by:
queryForLong in interface SimpleJdbcOperations
Throws:
DataAccessException

queryForObject

public <T> T queryForObject(String sql,
                            Class<T> requiredType,
                            Object... args)
                 throws DataAccessException
Description copied from interface: SimpleJdbcOperations
Query for an object of type T identified by the supplied @Class.

Specified by:
queryForObject in interface SimpleJdbcOperations
Parameters:
sql - the SQL query to run.
requiredType - the required type of the return value.
args - the args for the query.
Throws:
DataAccessException
See Also:
JdbcOperations.queryForObject(String, Class), JdbcOperations.queryForObject(String, Object[], Class)

queryForObject

public <T> T queryForObject(String sql,
                            ParameterizedRowMapper<T> rm,
                            Object... args)
                 throws DataAccessException
Description copied from interface: SimpleJdbcOperations
Query for an object of type T using the supplied ParameterizedRowMapper to the query results to the object.

Specified by:
queryForObject in interface SimpleJdbcOperations
Parameters:
sql - the SQL query to run.
rm - the @ParameterizedRowMapper to use for result mapping
args - the args for the query.
Throws:
DataAccessException
See Also:
JdbcOperations.queryForObject(String, org.springframework.jdbc.core.RowMapper), JdbcOperations.queryForObject(String, Object[], org.springframework.jdbc.core.RowMapper)

query

public <T> List<T> query(String sql,
                         ParameterizedRowMapper<T> rm,
                         Object... args)
              throws DataAccessException
Description copied from interface: SimpleJdbcOperations
Query for a List of Objects of type T using the supplied ParameterizedRowMapper to the query results to the object.

Specified by:
query in interface SimpleJdbcOperations
Parameters:
sql - the SQL query to run.
rm - the @ParameterizedRowMapper to use for result mapping
args - the args for the query.
Throws:
DataAccessException
See Also:
JdbcOperations.queryForObject(String, org.springframework.jdbc.core.RowMapper), JdbcOperations.queryForObject(String, Object[], org.springframework.jdbc.core.RowMapper)

queryForMap

public Map<String,Object> queryForMap(String sql,
                                      Object... args)
                               throws DataAccessException
Description copied from interface: SimpleJdbcOperations
Execute the supplied query with the (optional) supplied arguments.

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

Specified by:
queryForMap in interface SimpleJdbcOperations
Throws:
DataAccessException
See Also:
JdbcOperations.queryForMap(String), JdbcOperations.queryForMap(String, Object[])

queryForList

public List<Map<String,Object>> queryForList(String sql,
                                             Object... args)
                                      throws DataAccessException
Description copied from interface: SimpleJdbcOperations
Execute the supplied query with the (optional) supplied arguments.

Each element in the returned List is constructed as a Map as described in SimpleJdbcOperations.queryForMap(java.lang.String, java.lang.Object...)

Specified by:
queryForList in interface SimpleJdbcOperations
Throws:
DataAccessException
See Also:
JdbcOperations.queryForList(String), JdbcOperations.queryForList(String, Object[])

update

public int update(String sql,
                  Object... args)
           throws DataAccessException
Description copied from interface: SimpleJdbcOperations
Execute the supplied SQL statement with (optional) supplied arguments.

Specified by:
update in interface SimpleJdbcOperations
Returns:
the numbers of rows affected by the update.
Throws:
DataAccessException
See Also:
JdbcOperations.update(String), JdbcOperations.update(String, Object[])

The Spring Framework

Copyright © 2002-2007 The Spring Framework.