Batch Updates

From InterBase

Go Up to Programming with JDBC


Methods for the Statement and PreparedStatement Classes

The following methods have been added to both the Statement and the PreparedStatement classes. The methods listed below now work according to the JDBC specifications.

Methods for the Statement and PreparedStatement classes
Method Functionality

void Statement.addBatch(String sql)

Adds sql to the current list of commands.

void Statement.clearBatch()

Empties the list of commands for the current statement object.

int[] Statement.executeBatch()

throws BatchUpdateException

Submits the list of commands for this statement’s objects to the database for execution as a unit. The returned integer array contains the update counts for each of the SQL commands in the list.

void PreparedStatement.addBatch()

Adds a set of parameters to the list of commands for the current PreparedStatement object's list of commands to be sent to the database for execution.


The BatchUpdateException Class

A new BatchUpdateException class has been implemented in order to support JDBC Batch update functionality. Here is the list of methods and constructors in the new class:

Method/Constructor Functionality
public BatchUpdateException(
 String reason, 
 String SQLState, 
 int vendorCode, 
 int [] updateCounts)

Constructs a BatchUpdateException object where:

reason is a string describing the exception.

SQLState is an object containing Open Group code identification.

vendorCode identifies the vendor-specific database error code.

updateCounts contains an array of INT values where each element indicates the row count for each SQL UPDATE command that executed successfully before the exception was thrown.

public BatchUpdateException(
 String reason, 
 String SQLState, 
 int [] updateCounts)

Constructs a BatchUpdateException object where:

reason is a string describing the exception.

SQLState is an object containing the InterBase error code.

updateCounts contains an array of INT values where each element indicates the row count for each SQL UPDATE command that executed successfully before the exception was thrown.

The vendor code is implicitly set to zero.

public BatchUpdateException(
 String reason, 
 int [] updateCounts)

Constructs a BatchUpdateException object where:

reason is a string describing the exception.

updateCounts contains an array of INT values where each element indicates the row count for each SQL UPDATE command that executed successfully before the exception was thrown.

The following values are implicitly set: the vendorCode is set to zero and the Open Group code identification is set to null.

public BatchUpdateException (int [] updateCounts) 

Constructs a BatchUpdateException object where updateCounts contains an array of INT values in which each element indicates the row count for each SQL UPDATE command that executed successfully before the exception was thrown.

The following values are implicitly set: reason is set to null, vendorCode is set to zero, and the Open Group code identification is set to null.

public BatchUpdateException()

The following values are implicitly set:

updateCounts is set to a zero-length integer array.

reason is set to null.

vendorCode is set to zero.

the Open Group code identification is set to null.

public int [] getUpdateCounts()

Retrieves an array of INT values where each element indicates the row count for each SQL UPDATE command that executed successfully before the exception was thrown.

The DatabaseMetaData.supportsBatchUpdates Function

The DatabaseMetaData.supportsBatchUpdates function has changed as follows:

Function Functionality

boolean DatabaseMetaData.supportsBatchUpdates()

Can now return TRUE.


Code Examples

Code example for the batch update functions:

Statement Class
con.setAutoCommit(false);
Statement stmt = con.createStatement();
stmt.addBatch("INSERT INTO foo VALUES (1, 10));
stmt.addBatch("INSERT INTO foo VALUES (2, 21));
int[] updateCounts = pstmt.executeBatch();
con.commit();

Code example for the PreparedStatement class:

PreparedStatement pstmt = con.prepareStatement ("UPDATE employee set emp_id = ? where emp_id = ?")
pstmt.setInt(1, newEmpId1);
pstmt.setInt(2, oldEmpId1);
pstmt.addBatch();
pstmt.setInt(1, newEmpId2);
pstmt.setInt(2, oldEmpId2);
pstmt.addBatch();
int[] updateCounts = pstmt.executeBatch();

Code example for the BatchUpdateException class and getUpdateCounts() method

try
{
int[] updateCounts = pstmt.executeBatch();
}
catch (BatchUpdateException b)
{
int [] updates = b.getUpdateCounts();
for (int i = 0; i < updates.length; i++)
{
System.err.println ("Update Count " + updates[i]);
}
}

Advance To: