com.buzzsurf.sql
Class Delete

java.lang.Object
  extended by com.buzzsurf.sql.BuzzSQL
      extended by com.buzzsurf.sql.Update
          extended by com.buzzsurf.sql.Delete

public class Delete
extends Update

An Delete object deletes rows from a database table and queries the number of rows deleted. It is a combination of the underlying JDBC classes;

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

com.buzzsurf.sql.Delete extends com.buzzsurf.sql.Update, but add only minor functionality beyond that which is contained in the parent class. Therefore you may choose to use com.buzzsurf.sql.Update to perform deletes if it helps to streamline your object structure.

As an example, the following code fragment creates an Delete 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 delete count is queries by calling getDeleteCount(). The object is then closed to cleanup any resources and return the connection to the pool.
 Delete delete = new Delete();
 delete.setSQL("delete from example.table_example1 where col_int = ?");
 delete.setArgs(123);
 delete.execute();
 int deleteCount = update.getDeleteCount();
 delete.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, Update

Field Summary
 
Fields inherited from class com.buzzsurf.sql.Update
rowCount
 
Fields inherited from class com.buzzsurf.sql.BuzzSQL
args, con, DATABASE_FORMATTER, dataSourceName, sql, stmt, usingExplicitConnection
 
Constructor Summary
Delete()
          A zero argument constructor is provided for simplified operation with JavBeans, SOAP, and reflection scenarios where having such a constructor is necessary or convenient.
Delete(java.lang.String sql)
          A single argument constructor that accepts your SQL statement and uses the default DataSource.
Delete(java.lang.String sql, java.sql.Connection con)
          A dual argument constructor that accepts your SQL statement and a java.sql.Connection object.
Delete(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
 Delete execute()
          During execution a database connection is obtained (if needed), SQL and arguments are merged, and the PreparedStatement is executed against the database.
 int getDeleteCount()
          Get the number of rows deleted.
 
Methods inherited from class com.buzzsurf.sql.Update
getRowCount, getUpdateCount
 
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
 

Constructor Detail

Delete

public Delete()
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.


Delete

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

Parameters:
sql - The SQL statement

Delete

public Delete(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.

Delete

public Delete(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

getDeleteCount

public int getDeleteCount()
Get the number of rows deleted.

Returns:
Number of rows deleted

execute

public Delete 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.

Overrides:
execute in class Update
Throws:
java.sql.SQLException - if any of the executing JDBC operations failed