Code Examples
Go Up to Batch Updates
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]);
}
}