This class is concrete. Although it can be subclassed (for example
to add a custom update method) it can easily be parameterized by setting
SQL and declaring parameters.
| Constructor: |
public SqlUpdate() {
}
Constructor to allow use as a JavaBean. DataSource and SQL
must be supplied before compilation and use. |
public SqlUpdate(DataSource ds,
String sql) {
setDataSource(ds);
setSql(sql);
}
Constructs an update object with a given DataSource and SQL. Parameters:
ds - DataSource to use to obtain connections
sql - SQL statement to execute
|
public SqlUpdate(DataSource ds,
String sql,
int[] types) {
setDataSource(ds);
setSql(sql);
setTypes(types);
}
Construct an update object with a given DataSource, SQL
and anonymous parameters. Parameters:
ds - DataSource to use to obtain connections
sql - SQL statement to execute
types - SQL types of the parameters, as defined in the
java.sql.Types class
Also see:
- java.sql.Types
|
public SqlUpdate(DataSource ds,
String sql,
int[] types,
int maxRowsAffected) {
setDataSource(ds);
setSql(sql);
setTypes(types);
this.maxRowsAffected = maxRowsAffected;
}
Construct an update object with a given DataSource, SQL,
anonymous parameters and specifying the maximum number of rows
that may be affected. Parameters:
ds - DataSource to use to obtain connections
sql - SQL statement to execute
types - SQL types of the parameters, as defined in the
java.sql.Types class
maxRowsAffected - the maximum number of rows that may
be affected by the update
Also see:
- java.sql.Types
|
| Method from org.springframework.jdbc.object.SqlUpdate Detail: |
protected void checkRowsAffected(int rowsAffected) throws JdbcUpdateAffectedIncorrectNumberOfRowsException {
if (this.maxRowsAffected > 0 && rowsAffected > this.maxRowsAffected) {
throw new JdbcUpdateAffectedIncorrectNumberOfRowsException(getSql(), this.maxRowsAffected, rowsAffected);
}
if (this.requiredRowsAffected > 0 && rowsAffected != this.requiredRowsAffected) {
throw new JdbcUpdateAffectedIncorrectNumberOfRowsException(getSql(), this.requiredRowsAffected, rowsAffected);
}
}
Check the given number of affected rows against the
specified maximum number or required number. |
public void setMaxRowsAffected(int maxRowsAffected) {
this.maxRowsAffected = maxRowsAffected;
}
Set the maximum number of rows that may be affected by this update.
The default value is 0, which does not limit the number of rows affected. |
public void setRequiredRowsAffected(int requiredRowsAffected) {
this.requiredRowsAffected = requiredRowsAffected;
}
|
public int update() throws DataAccessException {
return update((Object[]) null);
}
Convenience method to execute an update with no parameters. |
public int update(Object[] params) throws DataAccessException {
validateParameters(params);
int rowsAffected = getJdbcTemplate().update(newPreparedStatementCreator(params));
checkRowsAffected(rowsAffected);
return rowsAffected;
}
Generic method to execute the update given parameters.
All other update methods invoke this method. |
public int update(int p1) throws DataAccessException {
return update(new Object[] {new Integer(p1)});
}
Convenient method to execute an update given one int arg. |
public int update(long p1) throws DataAccessException {
return update(new Object[] {new Long(p1)});
}
Convenient method to execute an update given one long arg. |
public int update(String p) throws DataAccessException {
return update(new Object[] {p});
}
Convenient method to execute an update given one String arg. |
public int update(Object[] params,
KeyHolder generatedKeyHolder) throws DataAccessException {
validateParameters(params);
int rowsAffected = getJdbcTemplate().update(newPreparedStatementCreator(params), generatedKeyHolder);
checkRowsAffected(rowsAffected);
return rowsAffected;
}
Method to execute the update given arguments and
retrieve the generated keys using a KeyHolder. |
public int update(int p1,
int p2) throws DataAccessException {
return update(new Object[] {new Integer(p1), new Integer(p2)});
}
Convenient method to execute an update given two int args. |
public int update(long p1,
long p2) throws DataAccessException {
return update(new Object[] {new Long(p1), new Long(p2)});
}
Convenient method to execute an update given two long args. |
public int update(String p1,
String p2) throws DataAccessException {
return update(new Object[] {p1, p2});
}
Convenient method to execute an update given two String args. |
public int updateByNamedParam(Map paramMap) throws DataAccessException {
validateNamedParameters(paramMap);
ParsedSql parsedSql = getParsedSql();
MapSqlParameterSource paramSource = new MapSqlParameterSource(paramMap);
String sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql, paramSource);
Object[] params = NamedParameterUtils.buildValueArray(parsedSql, paramSource, getDeclaredParameters());
int rowsAffected = getJdbcTemplate().update(newPreparedStatementCreator(sqlToUse, params));
checkRowsAffected(rowsAffected);
return rowsAffected;
}
Generic method to execute the update given named parameters.
All other update methods invoke this method. |
public int updateByNamedParam(Map paramMap,
KeyHolder generatedKeyHolder) throws DataAccessException {
validateNamedParameters(paramMap);
ParsedSql parsedSql = getParsedSql();
MapSqlParameterSource paramSource = new MapSqlParameterSource(paramMap);
String sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql, paramSource);
Object[] params = NamedParameterUtils.buildValueArray(parsedSql, paramSource, getDeclaredParameters());
int rowsAffected = getJdbcTemplate().update(newPreparedStatementCreator(sqlToUse, params), generatedKeyHolder);
checkRowsAffected(rowsAffected);
return rowsAffected;
}
Method to execute the update given arguments and
retrieve the generated keys using a KeyHolder. |