com.buzzsurf.sql
Class Update

java.lang.Object
  extended by com.buzzsurf.sql.BuzzSQL
      extended by com.buzzsurf.sql.Update
Direct Known Subclasses:
Delete, Insert

public class Update
extends BuzzSQL

An Update object modifies rows in a database and queries the number of rows updated. It is a combination of the underlying JDBC classes;

After execution, you may get the number of rows updated by calling getUpdateCount() or getRowCount().

As an example, the following code fragment creates an Update object using the automatic default connection to the database. The SQL statement is passed via constructor, and the arguments are passed using the setArgs(Object...) method. execute() is called to execute the database call, and then the update count is queries by calling getUpdateCount(). The object is then closed to cleanup any resources and return the connection to the pool.
 Update update = new Update();
 update.setSQL("update example.table_example1 set col_str = ? where col_pk = ?");
 update.setArgs("asdf",1);
 update.execute();
 int updateCount = update.getUpdateCount();
 update.close();
 
Note: This code does not handle exceptions. You normally should wrap your calls in try/catch/finally blocks to handle any problems. It is advisable to put your close() call in a finally block to insure the connection is always released.

Author:
Paul Cowan (www.buzzsurf.com/sql)
See Also:
BuzzSQL

Field Summary
protected  int rowCount
           
 
Fields inherited from class com.buzzsurf.sql.BuzzSQL
args, con, DATABASE_FORMATTER, dataSourceName, sql, stmt, usingExplicitConnection
 
Constructor Summary
Update()
          A zero argument constructor is provided for simplified operation with JavBeans, SOAP, and reflection scenarios where having such a constructor is necessary or convenient.
Update(java.lang.String sql)
          A single argument constructor that accepts your SQL statement and uses the default DataSource.
Update(java.lang.String sql, java.sql.Connection con)
          A dual argument constructor that accepts your SQL statement and a java.sql.Connection object.
Update(java.lang.String sql, java.lang.String dataSourceName)
          A dual argument constructor that accepts your SQL statement and the explicit name of a DataSource to use.
 
Method Summary
 Update execute()
          During execution a database connection is obtained (if needed), SQL and arguments are merged, and the PreparedStatement is executed against the database.
 int getRowCount()
          Get the number of rows updated.
 int getUpdateCount()
          Get the number of rows updated.
 
Methods inherited from class com.buzzsurf.sql.BuzzSQL
addArgs, close, close, getArgs, getConnection, getDataSourceName, getReleaseInfo, getSQL, getStatement, merge, prepare, queryToString, setArgs, setConnection, setDataSourceName, setSQL, usingExplicitConnection
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

rowCount

protected int rowCount
Constructor Detail

Update

public Update()
A zero argument constructor is provided for simplified operation with JavBeans, SOAP, and reflection scenarios where having such a constructor is necessary or convenient. At a minimum, you must call setSQL(String) before execution when using this constructor.


Update

public Update(java.lang.String sql)
A single argument constructor that accepts your SQL statement and uses the default DataSource.

Parameters:
sql - The SQL statement

Update

public Update(java.lang.String sql,
              java.lang.String dataSourceName)
A dual argument constructor that accepts your SQL statement and the explicit name of a DataSource to use.

Parameters:
sql - The SQL statement
dataSourceName - The explicit name of the dataSource to obtain a connecton from in DataSourceManager.

Update

public Update(java.lang.String sql,
              java.sql.Connection con)
A dual argument constructor that accepts your SQL statement and a java.sql.Connection object. This constructor provides a great deal of flexibility by allowing the use of "explicit" connections that are supplied by the user rather than being obtained automatically by BuzzSQL. Using a explicit connection also allows BuzzSQL to support database transactions.

Parameters:
sql - The SQL statement
con - The explicit database Connection
Method Detail

getRowCount

public int getRowCount()
Get the number of rows updated.

Returns:
Number of rows updated

getUpdateCount

public int getUpdateCount()
Get the number of rows updated.

Returns:
Number of rows updated

execute

public Update execute()
               throws java.sql.SQLException
During execution a database connection is obtained (if needed), SQL and arguments are merged, and the PreparedStatement is executed against the database. execute() throws an exception if any of these steps fails for any reason.

Typical post-execution steps are slightly different depending on the object subtype. Select based objects will obtain a ResultSet and ResultSetMetaData, while Update based objects will query and save the updated row count to a local variable.

execute() returns a reference to the current object to support method chaining. See Method Chaining for more information. You can assume execution succeeded if no exception is thrown.

Specified by:
execute in class BuzzSQL
Throws:
java.sql.SQLException - if any of the executing JDBC operations failed