Top Description Fields Constructors Methods
org.springframework.jdbc.object

public Class SqlUpdate

extends SqlOperation
Class Inheritance
Known Direct Subclasses
org.springframework.jdbc.object.BatchSqlUpdate
Imports
java.util.Map, javax.sql.DataSource, org.springframework.dao.DataAccessException, .InvalidDataAccessApiUsageException, org.springframework.jdbc.JdbcUpdateAffectedIncorrectNumberOfRowsException, org.springframework.jdbc.core.namedparam.MapSqlParameterSource, .NamedParameterUtils, .ParsedSql, org.springframework.jdbc.support.KeyHolder

Reusable operation object representing an SQL update.

This class provides a number of update methods, analogous to the execute methods of query objects.

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.

Like all RdbmsOperation classes that ship with the Spring Framework, SqlQuery instances are thread-safe after their initialization is complete. That is, after they are constructed and configured via their setter methods, they can be used safely from multiple threads.

Authors
Rod Johnson, Thomas Risberg, Juergen Hoeller
See Also
SqlQuery

Field Summary

Modifier and TypeField and Description
private int
maxRowsAffected

Maximum number of rows the update may affect.

private int
requiredRowsAffected

An exact number of rows that must be affected.

Constructor Summary

AccessConstructor and Description
public
SqlUpdate()

Constructor to allow use as a JavaBean.

public
SqlUpdate(DataSource
the DataSource to use to obtain connections
ds
,
String
the SQL statement to execute
sql
)

Constructs an update object with a given DataSource and SQL.

public
SqlUpdate(DataSource
the DataSource to use to obtain connections
ds
,
String
the SQL statement to execute
sql
,
int[]
the SQL types of the parameters, as defined in the java.sql.Types class
types
)

Construct an update object with a given DataSource, SQL and anonymous parameters.

public
SqlUpdate(DataSource
the DataSource to use to obtain connections
ds
,
String
the SQL statement to execute
sql
,
int[]
the SQL types of the parameters, as defined in the java.sql.Types class
types
,
int
the maximum number of rows that may be affected by the update
maxRowsAffected
)

Construct an update object with a given DataSource, SQL, anonymous parameters and specifying the maximum number of rows that may be affected.

Method Summary

Modifier and TypeMethod and Description
protected void
checkRowsAffected(int
the number of affected rows
rowsAffected
)

Check the given number of affected rows against the specified maximum number or required number.

public void
setMaxRowsAffected(int
the maximum number of rows that can be affected by this update without this class's update method considering it an error
maxRowsAffected
)

Set the maximum number of rows that may be affected by this update.

public void
setRequiredRowsAffected(int
the exact number of rows that must be affected by this update without this class's update method considering it an error
requiredRowsAffected
)

Set the exact number of rows that must be affected by this update.

public int

Returns:

the number of rows affected by the update
update
(Object...
array of parameters objects
params
)

Generic method to execute the update given parameters.

public int

Returns:

the number of rows affected by the update
update
(Object[]
array of parameter objects
params
,
KeyHolder
the KeyHolder that will hold the generated keys
generatedKeyHolder
)

Method to execute the update given arguments and retrieve the generated keys using a KeyHolder.

public int
update()

Convenience method to execute an update with no parameters.

public int
update(int p1)

Convenient method to execute an update given one int arg.

public int
update(int p1, int p2)

Convenient method to execute an update given two int args.

public int
update(long p1)

Convenient method to execute an update given one long arg.

public int
update(long p1, long p2)

Convenient method to execute an update given two long args.

public int
update(String p)

Convenient method to execute an update given one String arg.

public int
update(String p1, String p2)

Convenient method to execute an update given two String args.

public int

Returns:

the number of rows affected by the update
updateByNamedParam
(Map<String, ?>
a Map of parameter name to parameter object, matching named parameters specified in the SQL statement
paramMap
)

Generic method to execute the update given named parameters.

public int

Returns:

the number of rows affected by the update
updateByNamedParam
(Map<String, ?>
a Map of parameter name to parameter object, matching named parameters specified in the SQL statement
paramMap
,
KeyHolder
the KeyHolder that will hold the generated keys
generatedKeyHolder
)

Method to execute the update given arguments and retrieve the generated keys using a KeyHolder.

Inherited from org.springframework.jdbc.object.SqlOperation:
compileInternalgetParsedSqlnewPreparedStatementCreatornewPreparedStatementCreatornewPreparedStatementSetteronCompileInternal

Field Detail

maxRowsAffectedback to summary
private int maxRowsAffected

Maximum number of rows the update may affect. If more are affected, an exception will be thrown. Ignored if 0.

requiredRowsAffectedback to summary
private int requiredRowsAffected

An exact number of rows that must be affected. Ignored if 0.

Constructor Detail

SqlUpdateback to summary
public SqlUpdate()

Constructor to allow use as a JavaBean. DataSource and SQL must be supplied before compilation and use.

See Also
setDataSource, setSql
SqlUpdateback to summary
public SqlUpdate(DataSource ds, String sql)

Constructs an update object with a given DataSource and SQL.

Parameters
ds:DataSource

the DataSource to use to obtain connections

sql:String

the SQL statement to execute

SqlUpdateback to summary
public SqlUpdate(DataSource ds, String sql, int[] types)

Construct an update object with a given DataSource, SQL and anonymous parameters.

Parameters
ds:DataSource

the DataSource to use to obtain connections

sql:String

the SQL statement to execute

types:int[]

the SQL types of the parameters, as defined in the java.sql.Types class

See Also
java.sql.Types
SqlUpdateback to summary
public SqlUpdate(DataSource ds, String sql, int[] types, int 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

the DataSource to use to obtain connections

sql:String

the SQL statement to execute

types:int[]

the SQL types of the parameters, as defined in the java.sql.Types class

maxRowsAffected:int

the maximum number of rows that may be affected by the update

See Also
java.sql.Types

Method Detail

checkRowsAffectedback to summary
protected void checkRowsAffected(int rowsAffected) throws JdbcUpdateAffectedIncorrectNumberOfRowsException

Check the given number of affected rows against the specified maximum number or required number.

Parameters
rowsAffected:int

the number of affected rows

Exceptions
JdbcUpdateAffectedIncorrectNumberOfRowsException:
if the actually affected rows are out of bounds
See Also
setMaxRowsAffected, setRequiredRowsAffected
setMaxRowsAffectedback to summary
public void setMaxRowsAffected(int 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.

Parameters
maxRowsAffected:int

the maximum number of rows that can be affected by this update without this class's update method considering it an error

setRequiredRowsAffectedback to summary
public void setRequiredRowsAffected(int requiredRowsAffected)

Set the exact number of rows that must be affected by this update. The default value is 0, which allows any number of rows to be affected.

This is an alternative to setting the maximum number of rows that may be affected.

Parameters
requiredRowsAffected:int

the exact number of rows that must be affected by this update without this class's update method considering it an error

updateback to summary
public int update(Object... params) throws DataAccessException

Generic method to execute the update given parameters. All other update methods invoke this method.

Parameters
params:Object[]

array of parameters objects

Returns:int

the number of rows affected by the update

updateback to summary
public int update(Object[] params, KeyHolder generatedKeyHolder) throws DataAccessException

Method to execute the update given arguments and retrieve the generated keys using a KeyHolder.

Parameters
params:Object[]

array of parameter objects

generatedKeyHolder:KeyHolder

the KeyHolder that will hold the generated keys

Returns:int

the number of rows affected by the update

updateback to summary
public int update() throws DataAccessException

Convenience method to execute an update with no parameters.

updateback to summary
public int update(int p1) throws DataAccessException

Convenient method to execute an update given one int arg.

updateback to summary
public int update(int p1, int p2) throws DataAccessException

Convenient method to execute an update given two int args.

updateback to summary
public int update(long p1) throws DataAccessException

Convenient method to execute an update given one long arg.

updateback to summary
public int update(long p1, long p2) throws DataAccessException

Convenient method to execute an update given two long args.

updateback to summary
public int update(String p) throws DataAccessException

Convenient method to execute an update given one String arg.

updateback to summary
public int update(String p1, String p2) throws DataAccessException

Convenient method to execute an update given two String args.

updateByNamedParamback to summary
public int updateByNamedParam(Map<String, ?> paramMap) throws DataAccessException

Generic method to execute the update given named parameters. All other update methods invoke this method.

Parameters
paramMap:Map<String, ?>

a Map of parameter name to parameter object, matching named parameters specified in the SQL statement

Returns:int

the number of rows affected by the update

updateByNamedParamback to summary
public int updateByNamedParam(Map<String, ?> paramMap, KeyHolder generatedKeyHolder) throws DataAccessException

Method to execute the update given arguments and retrieve the generated keys using a KeyHolder.

Parameters
paramMap:Map<String, ?>

a Map of parameter name to parameter object, matching named parameters specified in the SQL statement

generatedKeyHolder:KeyHolder

the KeyHolder that will hold the generated keys

Returns:int

the number of rows affected by the update