Class NamedParameterJdbcTemplate
- All Implemented Interfaces:
NamedParameterJdbcOperations
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.
NOTE: An instance of this class is thread-safe once configured.
- Since:
- 2.0
- Author:
- Thomas Risberg, Juergen Hoeller
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intDefault maximum number of entries for this template's SQL cache: 256. -
Constructor Summary
ConstructorsConstructorDescriptionNamedParameterJdbcTemplate(DataSource dataSource) Create a new NamedParameterJdbcTemplate for the givenDataSource.NamedParameterJdbcTemplate(JdbcOperations classicJdbcTemplate) Create a new NamedParameterJdbcTemplate for the given classic SpringJdbcTemplate. -
Method Summary
Modifier and TypeMethodDescriptionint[]batchUpdate(String sql, Map<String, ?>[] batchValues) Executes a batch using the supplied SQL statement with the batch of supplied arguments.int[]batchUpdate(String sql, SqlParameterSource[] batchArgs) Execute a batch using the supplied SQL statement with the batch of supplied arguments.<T> Texecute(String sql, Map<String, ?> paramMap, PreparedStatementCallback<T> action) Execute a JDBC data access operation, implemented as callback action working on a JDBC PreparedStatement.<T> Texecute(String sql, SqlParameterSource paramSource, PreparedStatementCallback<T> action) Execute a JDBC data access operation, implemented as callback action working on a JDBC PreparedStatement.<T> Texecute(String sql, PreparedStatementCallback<T> action) Execute a JDBC data access operation, implemented as callback action working on a JDBC PreparedStatement.intReturn the maximum number of entries for this template's SQL cache.Expose the classic Spring JdbcTemplate operations to allow invocation of less commonly used methods.Expose the classic SpringJdbcTemplateitself, if available, in particular for passing it on to otherJdbcTemplateconsumers.protected ParsedSqlgetParsedSql(String sql) Obtain a parsed representation of the given SQL statement.protected PreparedStatementCreatorgetPreparedStatementCreator(String sql, SqlParameterSource paramSource) Build aPreparedStatementCreatorbased on the given SQL and named parameters.protected PreparedStatementCreatorgetPreparedStatementCreator(String sql, SqlParameterSource paramSource, Consumer<PreparedStatementCreatorFactory> customizer) Build aPreparedStatementCreatorbased on the given SQL and named parameters.protected PreparedStatementCreatorFactorygetPreparedStatementCreatorFactory(ParsedSql parsedSql, SqlParameterSource paramSource) Build aPreparedStatementCreatorFactorybased on the given SQL and named parameters.<T> Tquery(String sql, Map<String, ?> paramMap, ResultSetExtractor<T> 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.voidquery(String sql, Map<String, ?> 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.<T> List<T>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.<T> Tquery(String sql, SqlParameterSource paramSource, ResultSetExtractor<T> 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.voidquery(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.<T> List<T>query(String sql, SqlParameterSource paramSource, RowMapper<T> 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.<T> Tquery(String sql, ResultSetExtractor<T> rse) Query given SQL to create a prepared statement from SQL, reading the ResultSet with a ResultSetExtractor.voidquery(String sql, RowCallbackHandler rch) Query given SQL to create a prepared statement from SQL, reading the ResultSet on a per-row basis with a RowCallbackHandler.<T> List<T>Query given SQL to create a prepared statement from SQL, mapping each row to a Java object via a RowMapper.queryForList(String sql, Map<String, ?> 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.<T> List<T>queryForList(String sql, Map<String, ?> paramMap, Class<T> 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.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.<T> List<T>queryForList(String sql, SqlParameterSource paramSource, Class<T> 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.queryForMap(String sql, Map<String, ?> 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.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.<T> TqueryForObject(String sql, Map<String, ?> paramMap, Class<T> 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.<T> TqueryForObject(String sql, Map<String, ?> paramMap, RowMapper<T> 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.<T> TqueryForObject(String sql, SqlParameterSource paramSource, Class<T> 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.<T> TqueryForObject(String sql, SqlParameterSource paramSource, RowMapper<T> 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.queryForRowSet(String sql, Map<String, ?> paramMap) Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, expecting an 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 an SqlRowSet.<T> Stream<T>queryForStream(String sql, Map<String, ?> paramMap, RowMapper<T> 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, and turning it into an iterable and closeable Stream.<T> Stream<T>queryForStream(String sql, SqlParameterSource paramSource, RowMapper<T> 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, and turning it into an iterable and closeable Stream.voidsetCacheLimit(int cacheLimit) Specify the maximum number of entries for this template's SQL cache.intIssue an update via a prepared statement, binding the given arguments.intupdate(String sql, SqlParameterSource paramSource) Issue an update via a prepared statement, binding the given arguments.intupdate(String sql, SqlParameterSource paramSource, KeyHolder generatedKeyHolder) Issue an update via a prepared statement, binding the given arguments, returning generated keys.intupdate(String sql, SqlParameterSource paramSource, KeyHolder generatedKeyHolder, String[] keyColumnNames) Issue an update via a prepared statement, binding the given arguments, returning generated keys.
-
Field Details
-
DEFAULT_CACHE_LIMIT
public static final int DEFAULT_CACHE_LIMITDefault maximum number of entries for this template's SQL cache: 256.- See Also:
-
-
Constructor Details
-
NamedParameterJdbcTemplate
Create a new NamedParameterJdbcTemplate for the givenDataSource.Creates a classic Spring
JdbcTemplateand wraps it.- Parameters:
dataSource- the JDBC DataSource to access
-
NamedParameterJdbcTemplate
Create a new NamedParameterJdbcTemplate for the given classic SpringJdbcTemplate.- Parameters:
classicJdbcTemplate- the classic Spring JdbcTemplate to wrap
-
-
Method Details
-
getJdbcOperations
Expose the classic Spring JdbcTemplate operations to allow invocation of less commonly used methods.- Specified by:
getJdbcOperationsin interfaceNamedParameterJdbcOperations
-
getJdbcTemplate
Expose the classic SpringJdbcTemplateitself, if available, in particular for passing it on to otherJdbcTemplateconsumers.If sufficient for the purposes at hand,
getJdbcOperations()is recommended over this variant.- Since:
- 5.0.3
-
setCacheLimit
public void setCacheLimit(int cacheLimit) Specify the maximum number of entries for this template's SQL cache. Default is 256. 0 indicates no caching, always parsing each statement. -
getCacheLimit
public int getCacheLimit()Return the maximum number of entries for this template's SQL cache. -
execute
@Nullable public <T> T execute(String sql, SqlParameterSource paramSource, PreparedStatementCallback<T> action) throws DataAccessException Description copied from interface:NamedParameterJdbcOperationsExecute 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.
- Specified by:
executein interfaceNamedParameterJdbcOperations- Parameters:
sql- the SQL to executeparamSource- container of arguments to bind to the queryaction- callback object that specifies the action- Returns:
- a result object returned by the action, or
null - Throws:
DataAccessException- if there is any problem
-
execute
@Nullable public <T> T execute(String sql, Map<String, ?> paramMap, PreparedStatementCallback<T> action) throws DataAccessExceptionDescription copied from interface:NamedParameterJdbcOperationsExecute 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.
- Specified by:
executein interfaceNamedParameterJdbcOperations- Parameters:
sql- the 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- Returns:
- a result object returned by the action, or
null - Throws:
DataAccessException- if there is any problem
-
execute
@Nullable public <T> T execute(String sql, PreparedStatementCallback<T> action) throws DataAccessException Description copied from interface:NamedParameterJdbcOperationsExecute 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.
- Specified by:
executein interfaceNamedParameterJdbcOperations- Parameters:
sql- the SQL to executeaction- callback object that specifies the action- Returns:
- a result object returned by the action, or
null - Throws:
DataAccessException- if there is any problem
-
query
@Nullable public <T> T query(String sql, SqlParameterSource paramSource, ResultSetExtractor<T> rse) throws DataAccessException Description copied from interface:NamedParameterJdbcOperationsQuery 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.- Specified by:
queryin interfaceNamedParameterJdbcOperations- Parameters:
sql- the SQL query to executeparamSource- container of arguments to bind to the queryrse- object that will extract results- Returns:
- an arbitrary result object, as returned by the ResultSetExtractor
- Throws:
DataAccessException- if the query fails
-
query
@Nullable public <T> T query(String sql, Map<String, ?> paramMap, ResultSetExtractor<T> rse) throws DataAccessExceptionDescription copied from interface:NamedParameterJdbcOperationsQuery 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.- Specified by:
queryin interfaceNamedParameterJdbcOperations- Parameters:
sql- the 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- Returns:
- an arbitrary result object, as returned by the ResultSetExtractor
- Throws:
DataAccessException- if the query fails
-
query
Description copied from interface:NamedParameterJdbcOperationsQuery given SQL to create a prepared statement from SQL, reading the ResultSet with a ResultSetExtractor.Note: In contrast to the JdbcOperations method with the same signature, this query variant always uses a PreparedStatement. It is effectively equivalent to a query call with an empty parameter Map.
- Specified by:
queryin interfaceNamedParameterJdbcOperations- Parameters:
sql- the SQL query to executerse- object that will extract results- Returns:
- an arbitrary result object, as returned by the ResultSetExtractor
- Throws:
DataAccessException- if the query fails
-
query
public void query(String sql, SqlParameterSource paramSource, RowCallbackHandler rch) throws DataAccessException Description copied from interface:NamedParameterJdbcOperationsQuery 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.- Specified by:
queryin interfaceNamedParameterJdbcOperations- Parameters:
sql- the SQL query to executeparamSource- container of arguments to bind to the queryrch- object that will extract results, one row at a time- Throws:
DataAccessException- if the query fails
-
query
public void query(String sql, Map<String, ?> paramMap, RowCallbackHandler rch) throws DataAccessExceptionDescription copied from interface:NamedParameterJdbcOperationsQuery 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.- Specified by:
queryin interfaceNamedParameterJdbcOperations- Parameters:
sql- the 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- Throws:
DataAccessException- if the query fails
-
query
Description copied from interface:NamedParameterJdbcOperationsQuery given SQL to create a prepared statement from SQL, reading the ResultSet on a per-row basis with a RowCallbackHandler.Note: In contrast to the JdbcOperations method with the same signature, this query variant always uses a PreparedStatement. It is effectively equivalent to a query call with an empty parameter Map.
- Specified by:
queryin interfaceNamedParameterJdbcOperations- Parameters:
sql- the SQL query to executerch- object that will extract results, one row at a time- Throws:
DataAccessException- if the query fails
-
query
public <T> List<T> query(String sql, SqlParameterSource paramSource, RowMapper<T> rowMapper) throws DataAccessException Description copied from interface:NamedParameterJdbcOperationsQuery 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:
queryin interfaceNamedParameterJdbcOperations- Parameters:
sql- the SQL query to executeparamSource- container of arguments to bind to the queryrowMapper- object that will map one object per row- Returns:
- the result List, containing mapped objects
- Throws:
DataAccessException- if the query fails
-
query
public <T> List<T> query(String sql, Map<String, ?> paramMap, RowMapper<T> rowMapper) throws DataAccessExceptionDescription copied from interface:NamedParameterJdbcOperationsQuery 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:
queryin interfaceNamedParameterJdbcOperations- Parameters:
sql- the 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- Returns:
- the result List, containing mapped objects
- Throws:
DataAccessException- if the query fails
-
query
Description copied from interface:NamedParameterJdbcOperationsQuery given SQL to create a prepared statement from SQL, mapping each row to a Java object via a RowMapper.Note: In contrast to the JdbcOperations method with the same signature, this query variant always uses a PreparedStatement. It is effectively equivalent to a query call with an empty parameter Map.
- Specified by:
queryin interfaceNamedParameterJdbcOperations- Parameters:
sql- the SQL query to executerowMapper- object that will map one object per row- Returns:
- the result List, containing mapped objects
- Throws:
DataAccessException- if the query fails
-
queryForStream
public <T> Stream<T> queryForStream(String sql, SqlParameterSource paramSource, RowMapper<T> rowMapper) throws DataAccessException Description copied from interface:NamedParameterJdbcOperationsQuery 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, and turning it into an iterable and closeable Stream.- Specified by:
queryForStreamin interfaceNamedParameterJdbcOperations- Parameters:
sql- the SQL query to executeparamSource- container of arguments to bind to the queryrowMapper- object that will map one object per row- Returns:
- the result Stream, containing mapped objects, needing to be closed once fully processed (e.g. through a try-with-resources clause)
- Throws:
DataAccessException- if the query fails
-
queryForStream
public <T> Stream<T> queryForStream(String sql, Map<String, ?> paramMap, RowMapper<T> rowMapper) throws DataAccessExceptionDescription copied from interface:NamedParameterJdbcOperationsQuery 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, and turning it into an iterable and closeable Stream.- Specified by:
queryForStreamin interfaceNamedParameterJdbcOperations- Parameters:
sql- the 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- Returns:
- the result Stream, containing mapped objects, needing to be closed once fully processed (e.g. through a try-with-resources clause)
- Throws:
DataAccessException- if the query fails
-
queryForObject
@Nullable public <T> T queryForObject(String sql, SqlParameterSource paramSource, RowMapper<T> rowMapper) throws DataAccessException Description copied from interface:NamedParameterJdbcOperationsQuery 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.- Specified by:
queryForObjectin interfaceNamedParameterJdbcOperations- Parameters:
sql- the SQL query to executeparamSource- container of arguments to bind to the queryrowMapper- object that will map one object per row- Returns:
- the single mapped object (may be
nullif the givenRowMapperreturned null) - Throws:
IncorrectResultSizeDataAccessException- if the query does not return exactly one rowDataAccessException- if the query fails
-
queryForObject
@Nullable public <T> T queryForObject(String sql, Map<String, ?> paramMap, RowMapper<T> rowMapper) throws DataAccessExceptionDescription copied from interface:NamedParameterJdbcOperationsQuery 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.- Specified by:
queryForObjectin interfaceNamedParameterJdbcOperations- Parameters:
sql- the 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- Returns:
- the single mapped object (may be
nullif the givenRowMapperreturned null) - Throws:
IncorrectResultSizeDataAccessException- if the query does not return exactly one rowDataAccessException- if the query fails
-
queryForObject
@Nullable public <T> T queryForObject(String sql, SqlParameterSource paramSource, Class<T> requiredType) throws DataAccessException Description copied from interface:NamedParameterJdbcOperationsQuery given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, expecting a result object.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:
queryForObjectin interfaceNamedParameterJdbcOperations- Parameters:
sql- the SQL query to executeparamSource- container of arguments to bind to the queryrequiredType- the type that the result object is expected to match- Returns:
- the result object of the required type, or
nullin case of SQL NULL - Throws:
IncorrectResultSizeDataAccessException- if the query does not return exactly one rowIncorrectResultSetColumnCountException- if the query does not return a row containing a single columnDataAccessException- if the query fails- See Also:
-
queryForObject
@Nullable public <T> T queryForObject(String sql, Map<String, ?> paramMap, Class<T> requiredType) throws DataAccessExceptionDescription copied from interface:NamedParameterJdbcOperationsQuery given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, expecting a result object.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:
queryForObjectin interfaceNamedParameterJdbcOperations- Parameters:
sql- the 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- Returns:
- the result object of the required type, or
nullin case of SQL NULL - Throws:
IncorrectResultSizeDataAccessException- if the query does not return exactly one rowIncorrectResultSetColumnCountException- if the query does not return a row containing a single columnDataAccessException- if the query fails- See Also:
-
queryForMap
public Map<String,Object> queryForMap(String sql, SqlParameterSource paramSource) throws DataAccessException Description copied from interface:NamedParameterJdbcOperationsQuery given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, expecting a result Map.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:
queryForMapin interfaceNamedParameterJdbcOperations- Parameters:
sql- the SQL query to executeparamSource- container of arguments to bind to the query- Returns:
- the result Map (one entry for each column, using the column name as the key)
- Throws:
IncorrectResultSizeDataAccessException- if the query does not return exactly one rowDataAccessException- if the query fails- See Also:
-
queryForMap
public Map<String,Object> queryForMap(String sql, Map<String, ?> paramMap) throws DataAccessExceptionDescription copied from interface:NamedParameterJdbcOperationsQuery 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).
- Specified by:
queryForMapin interfaceNamedParameterJdbcOperations- Parameters:
sql- the SQL query to executeparamMap- map of parameters to bind to the query (leaving it to the PreparedStatement to guess the corresponding SQL type)- Returns:
- the result Map (one entry for each column, using the column name as the key)
- Throws:
IncorrectResultSizeDataAccessException- if the query does not return exactly one rowDataAccessException- if the query fails- See Also:
-
queryForList
public <T> List<T> queryForList(String sql, SqlParameterSource paramSource, Class<T> elementType) throws DataAccessException Description copied from interface:NamedParameterJdbcOperationsQuery 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 result objects, each of them matching the specified element type.
- Specified by:
queryForListin interfaceNamedParameterJdbcOperations- Parameters:
sql- the 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)- Returns:
- a List of objects that match the specified element type
- Throws:
DataAccessException- if the query fails- See Also:
-
queryForList
public <T> List<T> queryForList(String sql, Map<String, ?> paramMap, Class<T> elementType) throws DataAccessExceptionDescription copied from interface:NamedParameterJdbcOperationsQuery 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 result objects, each of them matching the specified element type.
- Specified by:
queryForListin interfaceNamedParameterJdbcOperations- Parameters:
sql- the 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)- Returns:
- a List of objects that match the specified element type
- Throws:
DataAccessException- if the query fails- See Also:
-
queryForList
public List<Map<String,Object>> queryForList(String sql, SqlParameterSource paramSource) throws DataAccessException Description copied from interface:NamedParameterJdbcOperationsQuery 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
queryForMapmethods.- Specified by:
queryForListin interfaceNamedParameterJdbcOperations- Parameters:
sql- the SQL query to executeparamSource- container of arguments to bind to the query- Returns:
- a List that contains a Map per row
- Throws:
DataAccessException- if the query fails- See Also:
-
queryForList
public List<Map<String,Object>> queryForList(String sql, Map<String, ?> paramMap) throws DataAccessExceptionDescription copied from interface:NamedParameterJdbcOperationsQuery 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
queryForMapmethods.- Specified by:
queryForListin interfaceNamedParameterJdbcOperations- Parameters:
sql- the SQL query to executeparamMap- map of parameters to bind to the query (leaving it to the PreparedStatement to guess the corresponding SQL type)- Returns:
- a List that contains a Map per row
- Throws:
DataAccessException- if the query fails- See Also:
-
queryForRowSet
public SqlRowSet queryForRowSet(String sql, SqlParameterSource paramSource) throws DataAccessException Description copied from interface:NamedParameterJdbcOperationsQuery given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, expecting an 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, for the default implementation, JDBC RowSet support needs to be available at runtime: by default, Sun's
com.sun.rowset.CachedRowSetImplclass is used, which is part of JDK 1.5+ and also available separately as part of Sun's JDBC RowSet Implementations download (rowset.jar).- Specified by:
queryForRowSetin interfaceNamedParameterJdbcOperations- Parameters:
sql- the SQL query to executeparamSource- container of arguments to bind to the query- Returns:
- an SqlRowSet representation (possibly a wrapper around a
javax.sql.rowset.CachedRowSet) - Throws:
DataAccessException- if there is any problem executing the query- See Also:
-
queryForRowSet
Description copied from interface:NamedParameterJdbcOperationsQuery given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, expecting an 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, for the default implementation, JDBC RowSet support needs to be available at runtime: by default, Sun's
com.sun.rowset.CachedRowSetImplclass is used, which is part of JDK 1.5+ and also available separately as part of Sun's JDBC RowSet Implementations download (rowset.jar).- Specified by:
queryForRowSetin interfaceNamedParameterJdbcOperations- Parameters:
sql- the SQL query to executeparamMap- map of parameters to bind to the query (leaving it to the PreparedStatement to guess the corresponding SQL type)- Returns:
- an SqlRowSet representation (possibly a wrapper around a
javax.sql.rowset.CachedRowSet) - Throws:
DataAccessException- if there is any problem executing the query- See Also:
-
update
Description copied from interface:NamedParameterJdbcOperationsIssue an update via a prepared statement, binding the given arguments.- Specified by:
updatein interfaceNamedParameterJdbcOperations- Parameters:
sql- the SQL containing named parametersparamSource- container of arguments and SQL types to bind to the query- Returns:
- the number of rows affected
- Throws:
DataAccessException- if there is any problem issuing the update
-
update
Description copied from interface:NamedParameterJdbcOperationsIssue an update via a prepared statement, binding the given arguments.- Specified by:
updatein interfaceNamedParameterJdbcOperations- Parameters:
sql- the SQL containing named parametersparamMap- map of parameters to bind to the query (leaving it to the PreparedStatement to guess the corresponding SQL type)- Returns:
- the number of rows affected
- Throws:
DataAccessException- if there is any problem issuing the update
-
update
public int update(String sql, SqlParameterSource paramSource, KeyHolder generatedKeyHolder) throws DataAccessException Description copied from interface:NamedParameterJdbcOperationsIssue an update via a prepared statement, binding the given arguments, returning generated keys.- Specified by:
updatein interfaceNamedParameterJdbcOperations- Parameters:
sql- the SQL containing named parametersparamSource- container of arguments and SQL types to bind to the querygeneratedKeyHolder- aKeyHolderthat will hold the generated keys- Returns:
- the number of rows affected
- Throws:
DataAccessException- if there is any problem issuing the update- See Also:
-
update
public int update(String sql, SqlParameterSource paramSource, KeyHolder generatedKeyHolder, @Nullable String[] keyColumnNames) throws DataAccessException Description copied from interface:NamedParameterJdbcOperationsIssue an update via a prepared statement, binding the given arguments, returning generated keys.- Specified by:
updatein interfaceNamedParameterJdbcOperations- Parameters:
sql- the SQL containing named parametersparamSource- container of arguments and SQL types to bind to the querygeneratedKeyHolder- aKeyHolderthat will hold the generated keyskeyColumnNames- names of the columns that will have keys generated for them- Returns:
- the number of rows affected
- Throws:
DataAccessException- if there is any problem issuing the update- See Also:
-
batchUpdate
Description copied from interface:NamedParameterJdbcOperationsExecutes a batch using the supplied SQL statement with the batch of supplied arguments.- Specified by:
batchUpdatein interfaceNamedParameterJdbcOperations- Parameters:
sql- the SQL statement to executebatchValues- the array of Maps containing the batch of arguments for the query- Returns:
- an array containing the numbers of rows affected by each update in the batch
(may also contain special JDBC-defined negative values for affected rows such as
Statement.SUCCESS_NO_INFO/Statement.EXECUTE_FAILED)
-
batchUpdate
Description copied from interface:NamedParameterJdbcOperationsExecute a batch using the supplied SQL statement with the batch of supplied arguments.- Specified by:
batchUpdatein interfaceNamedParameterJdbcOperations- Parameters:
sql- the SQL statement to executebatchArgs- the array ofSqlParameterSourcecontaining the batch of arguments for the query- Returns:
- an array containing the numbers of rows affected by each update in the batch
(may also contain special JDBC-defined negative values for affected rows such as
Statement.SUCCESS_NO_INFO/Statement.EXECUTE_FAILED)
-
getPreparedStatementCreator
protected PreparedStatementCreator getPreparedStatementCreator(String sql, SqlParameterSource paramSource) Build aPreparedStatementCreatorbased on the given SQL and named parameters.Note: Directly called from all
queryvariants. Delegates to the commongetPreparedStatementCreator(String, SqlParameterSource, Consumer)method.- Parameters:
sql- the SQL statement to executeparamSource- container of arguments to bind- Returns:
- the corresponding
PreparedStatementCreator - See Also:
-
getPreparedStatementCreator
protected PreparedStatementCreator getPreparedStatementCreator(String sql, SqlParameterSource paramSource, @Nullable Consumer<PreparedStatementCreatorFactory> customizer) Build aPreparedStatementCreatorbased on the given SQL and named parameters.Note: Used for the
updatevariant with generated key handling, and also delegated fromgetPreparedStatementCreator(String, SqlParameterSource).- Parameters:
sql- the SQL statement to executeparamSource- container of arguments to bindcustomizer- callback for setting further properties on thePreparedStatementCreatorFactoryin use), applied before the actualnewPreparedStatementCreatorcall- Returns:
- the corresponding
PreparedStatementCreator - Since:
- 5.0.5
- See Also:
-
getParsedSql
Obtain a parsed representation of the given SQL statement.The default implementation uses an LRU cache with an upper limit of 256 entries.
- Parameters:
sql- the original SQL statement- Returns:
- a representation of the parsed SQL statement
-
getPreparedStatementCreatorFactory
protected PreparedStatementCreatorFactory getPreparedStatementCreatorFactory(ParsedSql parsedSql, SqlParameterSource paramSource) Build aPreparedStatementCreatorFactorybased on the given SQL and named parameters.- Parameters:
parsedSql- parsed representation of the given SQL statementparamSource- container of arguments to bind- Returns:
- the corresponding
PreparedStatementCreatorFactory - Since:
- 5.1.3
- See Also:
-