|
The Spring Framework | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate
public class NamedParameterJdbcTemplate
Template class with a basic set of JDBC operations, allowing the use of named parameters rather than traditional '?' placeholders.
This class delegates to a wrapped JdbcTemplate
once the substitution from named parameters to JDBC style '?' placeholders is
done at execution time. It also allows for expanding a List
of values to the appropriate number of placeholders.
The underlying JdbcTemplate
is
exposed to allow for convenient access to the traditional
JdbcTemplate
methods.
NamedParameterJdbcOperations
,
JdbcTemplate
Constructor Summary | |
---|---|
NamedParameterJdbcTemplate(DataSource dataSource)
Create a new NamedParameterJdbcTemplate for the given DataSource . |
|
NamedParameterJdbcTemplate(JdbcOperations classicJdbcTemplate)
Create a new NamedParameterJdbcTemplate for the given classic Spring JdbcTemplate . |
Method Summary | |
---|---|
Object |
execute(String sql,
Map paramMap,
PreparedStatementCallback action)
Execute a JDBC data access operation, implemented as callback action working on a JDBC PreparedStatement. |
Object |
execute(String sql,
SqlParameterSource paramSource,
PreparedStatementCallback action)
Execute a JDBC data access operation, implemented as callback action working on a JDBC PreparedStatement. |
JdbcOperations |
getJdbcOperations()
Expose the classic Spring JdbcTemplate to allow invocation of less commonly used methods. |
protected ParsedSql |
getParsedSql(String sql)
Obtain a parsed representation of the given SQL statement. |
protected PreparedStatementCreator |
getPreparedStatementCreator(String sql,
SqlParameterSource paramSource)
Build a PreparedStatementCreator based on the given SQL and named parameters. |
Object |
query(String sql,
Map paramMap,
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. |
void |
query(String sql,
Map paramMap,
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. |
List |
query(String sql,
Map paramMap,
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,
SqlParameterSource paramSource,
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. |
void |
query(String sql,
SqlParameterSource paramSource,
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. |
List |
query(String sql,
SqlParameterSource paramSource,
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. |
int |
queryForInt(String sql,
Map paramMap)
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,
SqlParameterSource paramSource)
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,
Map paramMap)
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,
Map paramMap,
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. |
List |
queryForList(String sql,
SqlParameterSource paramSource)
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,
SqlParameterSource paramSource,
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. |
long |
queryForLong(String sql,
Map paramMap)
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,
SqlParameterSource paramSource)
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. |
Map |
queryForMap(String sql,
Map paramMap)
Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, expecting a result Map. |
Map |
queryForMap(String sql,
SqlParameterSource paramSource)
Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, expecting a result Map. |
Object |
queryForObject(String sql,
Map paramMap,
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,
Map paramMap,
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. |
Object |
queryForObject(String sql,
SqlParameterSource paramSource,
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,
SqlParameterSource paramSource,
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. |
SqlRowSet |
queryForRowSet(String sql,
Map paramMap)
Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, expecting a SqlRowSet. |
SqlRowSet |
queryForRowSet(String sql,
SqlParameterSource paramSource)
Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, expecting a SqlRowSet. |
int |
update(String sql,
Map paramMap)
Issue an update via a prepared statement, binding the given arguments. |
int |
update(String sql,
SqlParameterSource paramSource)
Issue an update via a prepared statement, binding the given arguments. |
int |
update(String sql,
SqlParameterSource paramSource,
KeyHolder generatedKeyHolder)
Issue an update via a prepared statement, binding the given arguments, returning generated keys. |
int |
update(String sql,
SqlParameterSource paramSource,
KeyHolder generatedKeyHolder,
String[] keyColumnNames)
Issue an update via a prepared statement, binding the given arguments, returning generated keys. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public NamedParameterJdbcTemplate(DataSource dataSource)
DataSource
.
Creates a classic Spring JdbcTemplate
and wraps it.
dataSource
- the JDBC DataSource to accesspublic NamedParameterJdbcTemplate(JdbcOperations classicJdbcTemplate)
JdbcTemplate
.
classicJdbcTemplate
- the classic Spring JdbcTemplate to wrapMethod Detail |
---|
public JdbcOperations getJdbcOperations()
getJdbcOperations
in interface NamedParameterJdbcOperations
public Object execute(String sql, SqlParameterSource paramSource, PreparedStatementCallback action) throws DataAccessException
NamedParameterJdbcOperations
The callback action can return a result object, for example a domain object or a collection of domain objects.
execute
in interface NamedParameterJdbcOperations
sql
- SQL to executeparamSource
- container of arguments to bind to the queryaction
- callback object that specifies the action
null
DataAccessException
- if there is any problempublic Object execute(String sql, Map paramMap, PreparedStatementCallback action) throws DataAccessException
NamedParameterJdbcOperations
The callback action can return a result object, for example a domain object or a collection of domain objects.
execute
in interface NamedParameterJdbcOperations
sql
- SQL to executeparamMap
- map of parameters to bind to the query
(leaving it to the PreparedStatement to guess the corresponding SQL type)action
- callback object that specifies the action
null
DataAccessException
- if there is any problempublic Object query(String sql, SqlParameterSource paramSource, ResultSetExtractor rse) throws DataAccessException
NamedParameterJdbcOperations
query
in interface NamedParameterJdbcOperations
sql
- SQL query to executeparamSource
- container of arguments to bind to the queryrse
- object that will extract results
DataAccessException
- if the query failspublic Object query(String sql, Map paramMap, ResultSetExtractor rse) throws DataAccessException
NamedParameterJdbcOperations
query
in interface NamedParameterJdbcOperations
sql
- SQL query to executeparamMap
- map of parameters to bind to the query
(leaving it to the PreparedStatement to guess the corresponding SQL type)rse
- object that will extract results
DataAccessException
- if the query failspublic void query(String sql, SqlParameterSource paramSource, RowCallbackHandler rch) throws DataAccessException
NamedParameterJdbcOperations
query
in interface NamedParameterJdbcOperations
sql
- SQL query to executeparamSource
- container of arguments to bind to the queryrch
- object that will extract results, one row at a time
DataAccessException
- if the query failspublic void query(String sql, Map paramMap, RowCallbackHandler rch) throws DataAccessException
NamedParameterJdbcOperations
query
in interface NamedParameterJdbcOperations
sql
- SQL query to executeparamMap
- map of parameters to bind to the query
(leaving it to the PreparedStatement to guess the corresponding SQL type)rch
- object that will extract results, one row at a time
DataAccessException
- if the query failspublic List query(String sql, SqlParameterSource paramSource, RowMapper rowMapper) throws DataAccessException
NamedParameterJdbcOperations
query
in interface NamedParameterJdbcOperations
sql
- SQL query to executeparamSource
- container of arguments to bind to the queryrowMapper
- object that will map one object per row
DataAccessException
- if the query failspublic List query(String sql, Map paramMap, RowMapper rowMapper) throws DataAccessException
NamedParameterJdbcOperations
query
in interface NamedParameterJdbcOperations
sql
- SQL query to executeparamMap
- map of parameters to bind to the query
(leaving it to the PreparedStatement to guess the corresponding SQL type)rowMapper
- object that will map one object per row
DataAccessException
- if the query failspublic Object queryForObject(String sql, SqlParameterSource paramSource, RowMapper rowMapper) throws DataAccessException
NamedParameterJdbcOperations
queryForObject
in interface NamedParameterJdbcOperations
sql
- SQL query to executeparamSource
- container of arguments to bind to the queryrowMapper
- object that will map one object per row
IncorrectResultSizeDataAccessException
- if the query does not return exactly one row, or does not return exactly
one column in that row
DataAccessException
- if the query failspublic Object queryForObject(String sql, Map paramMap, RowMapper rowMapper) throws DataAccessException
NamedParameterJdbcOperations
queryForObject
in interface NamedParameterJdbcOperations
sql
- SQL query to executeparamMap
- map of parameters to bind to the query
(leaving it to the PreparedStatement to guess the corresponding SQL type)rowMapper
- object that will map one object per row
IncorrectResultSizeDataAccessException
- if the query does not return exactly one row, or does not return exactly
one column in that row
DataAccessException
- if the query failspublic Object queryForObject(String sql, SqlParameterSource paramSource, Class requiredType) throws DataAccessException
NamedParameterJdbcOperations
The query is expected to be a single row/single column query; the returned result will be directly mapped to the corresponding object type.
queryForObject
in interface NamedParameterJdbcOperations
sql
- SQL query to executeparamSource
- container of arguments to bind to the queryrequiredType
- the type that the result object is expected to match
null
in case of SQL NULL
IncorrectResultSizeDataAccessException
- if the query does not return exactly one row, or does not return exactly
one column in that row
DataAccessException
- if the query failsJdbcTemplate.queryForObject(String, Class)
public Object queryForObject(String sql, Map paramMap, Class requiredType) throws DataAccessException
NamedParameterJdbcOperations
The query is expected to be a single row/single column query; the returned result will be directly mapped to the corresponding object type.
queryForObject
in interface NamedParameterJdbcOperations
sql
- SQL query to executeparamMap
- map of parameters to bind to the query
(leaving it to the PreparedStatement to guess the corresponding SQL type)requiredType
- the type that the result object is expected to match
null
in case of SQL NULL
IncorrectResultSizeDataAccessException
- if the query does not return exactly one row, or does not return exactly
one column in that row
DataAccessException
- if the query failsJdbcTemplate.queryForObject(String, Class)
public Map queryForMap(String sql, SqlParameterSource paramSource) throws DataAccessException
NamedParameterJdbcOperations
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).
queryForMap
in interface NamedParameterJdbcOperations
sql
- SQL query to executeparamSource
- container of arguments to bind to the query
IncorrectResultSizeDataAccessException
- if the query does not return exactly one row, or does not return exactly
one column in that row
DataAccessException
- if the query failsJdbcTemplate.queryForMap(String)
,
ColumnMapRowMapper
public Map queryForMap(String sql, Map paramMap) throws DataAccessException
NamedParameterJdbcOperations
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).
queryForMap
in interface NamedParameterJdbcOperations
sql
- SQL query to executeparamMap
- map of parameters to bind to the query
(leaving it to the PreparedStatement to guess the corresponding SQL type)
IncorrectResultSizeDataAccessException
- if the query does not return exactly one row, or does not return exactly
one column in that row
DataAccessException
- if the query failsJdbcTemplate.queryForMap(String)
,
ColumnMapRowMapper
public long queryForLong(String sql, SqlParameterSource paramSource) throws DataAccessException
NamedParameterJdbcOperations
The query is expected to be a single row/single column query that results in a long value.
queryForLong
in interface NamedParameterJdbcOperations
sql
- SQL query to executeparamSource
- container of arguments to bind to the query
IncorrectResultSizeDataAccessException
- if the query does not return exactly one row, or does not return exactly
one column in that row
DataAccessException
- if the query failsJdbcTemplate.queryForLong(String)
public long queryForLong(String sql, Map paramMap) throws DataAccessException
NamedParameterJdbcOperations
The query is expected to be a single row/single column query that results in a long value.
queryForLong
in interface NamedParameterJdbcOperations
sql
- SQL query to executeparamMap
- map of parameters to bind to the query
(leaving it to the PreparedStatement to guess the corresponding SQL type)
IncorrectResultSizeDataAccessException
- if the query does not return exactly one row, or does not return exactly
one column in that row
DataAccessException
- if the query failsJdbcTemplate.queryForLong(String)
public int queryForInt(String sql, SqlParameterSource paramSource) throws DataAccessException
NamedParameterJdbcOperations
The query is expected to be a single row/single column query that results in an int value.
queryForInt
in interface NamedParameterJdbcOperations
sql
- SQL query to executeparamSource
- container of arguments to bind to the query
IncorrectResultSizeDataAccessException
- if the query does not return
exactly one row, or does not return exactly one column in that row
DataAccessException
- if the query failsJdbcTemplate.queryForInt(String)
public int queryForInt(String sql, Map paramMap) throws DataAccessException
NamedParameterJdbcOperations
The query is expected to be a single row/single column query that results in an int value.
queryForInt
in interface NamedParameterJdbcOperations
sql
- SQL query to executeparamMap
- map of parameters to bind to the query
(leaving it to the PreparedStatement to guess the corresponding SQL type)
IncorrectResultSizeDataAccessException
- if the query does not return
exactly one row, or does not return exactly one column in that row
DataAccessException
- if the query failsJdbcTemplate.queryForInt(String)
public List queryForList(String sql, SqlParameterSource paramSource, Class elementType) throws DataAccessException
NamedParameterJdbcOperations
The results will be mapped to a List (one entry for each row) of result objects, each of them matching the specified element type.
queryForList
in interface NamedParameterJdbcOperations
sql
- SQL query to executeparamSource
- container of arguments to bind to the queryelementType
- the required type of element in the result list
(for example, Integer.class
)
DataAccessException
- if the query failsJdbcTemplate.queryForList(String, Class)
,
SingleColumnRowMapper
public List queryForList(String sql, Map paramMap, Class elementType) throws DataAccessException
NamedParameterJdbcOperations
The results will be mapped to a List (one entry for each row) of result objects, each of them matching the specified element type.
queryForList
in interface NamedParameterJdbcOperations
sql
- SQL query to executeparamMap
- map of parameters to bind to the query
(leaving it to the PreparedStatement to guess the corresponding SQL type)elementType
- the required type of element in the result list
(for example, Integer.class
)
DataAccessException
- if the query failsJdbcTemplate.queryForList(String, Class)
,
SingleColumnRowMapper
public List queryForList(String sql, SqlParameterSource paramSource) throws DataAccessException
NamedParameterJdbcOperations
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
in interface NamedParameterJdbcOperations
sql
- SQL query to executeparamSource
- container of arguments to bind to the query
DataAccessException
- if the query failsJdbcTemplate.queryForList(String)
public List queryForList(String sql, Map paramMap) throws DataAccessException
NamedParameterJdbcOperations
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.
queryForList
in interface NamedParameterJdbcOperations
sql
- SQL query to executeparamMap
- map of parameters to bind to the query
(leaving it to the PreparedStatement to guess the corresponding SQL type)
DataAccessException
- if the query failsJdbcTemplate.queryForList(String)
public SqlRowSet queryForRowSet(String sql, SqlParameterSource paramSource) throws DataAccessException
NamedParameterJdbcOperations
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
in interface NamedParameterJdbcOperations
sql
- SQL query to executeparamSource
- container of arguments to bind to the query
javax.sql.rowset.CachedRowSet
)
DataAccessException
- if there is any problem executing the queryJdbcTemplate.queryForRowSet(String)
,
SqlRowSetResultSetExtractor
,
CachedRowSet
public SqlRowSet queryForRowSet(String sql, Map paramMap) throws DataAccessException
NamedParameterJdbcOperations
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
in interface NamedParameterJdbcOperations
sql
- SQL query to executeparamMap
- map of parameters to bind to the query
(leaving it to the PreparedStatement to guess the corresponding SQL type)
javax.sql.rowset.CachedRowSet
)
DataAccessException
- if there is any problem executing the queryJdbcTemplate.queryForRowSet(String)
,
SqlRowSetResultSetExtractor
,
CachedRowSet
public int update(String sql, SqlParameterSource paramSource) throws DataAccessException
NamedParameterJdbcOperations
update
in interface NamedParameterJdbcOperations
sql
- SQL containing named parametersparamSource
- container of arguments and SQL types to bind to the query
DataAccessException
- if there is any problem issuing the updatepublic int update(String sql, Map paramMap) throws DataAccessException
NamedParameterJdbcOperations
update
in interface NamedParameterJdbcOperations
sql
- SQL containing named parametersparamMap
- map of parameters to bind to the query
(leaving it to the PreparedStatement to guess the corresponding SQL type)
DataAccessException
- if there is any problem issuing the updatepublic int update(String sql, SqlParameterSource paramSource, KeyHolder generatedKeyHolder) throws DataAccessException
NamedParameterJdbcOperations
update
in interface NamedParameterJdbcOperations
sql
- SQL containing named parametersparamSource
- container of arguments and SQL types to bind to the querygeneratedKeyHolder
- KeyHolder that will hold the generated keys
DataAccessException
- if there is any problem issuing the updateMapSqlParameterSource
,
GeneratedKeyHolder
public int update(String sql, SqlParameterSource paramSource, KeyHolder generatedKeyHolder, String[] keyColumnNames) throws DataAccessException
NamedParameterJdbcOperations
update
in interface NamedParameterJdbcOperations
sql
- SQL containing named parametersparamSource
- container of arguments and SQL types to bind to the querygeneratedKeyHolder
- KeyHolder that will hold the generated keyskeyColumnNames
- names of the columns that will have keys generated for them
DataAccessException
- if there is any problem issuing the updateMapSqlParameterSource
,
GeneratedKeyHolder
protected PreparedStatementCreator getPreparedStatementCreator(String sql, SqlParameterSource paramSource)
Note: Not used for the update
variant with generated key handling.
sql
- SQL to executeparamSource
- container of arguments to bind
protected ParsedSql getParsedSql(String sql)
sql
- the original SQL
|
The Spring Framework | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |