I am inserting records in a table in Db2-400 (Db2 i)
database using JDBC
prepared statement
batch. Table has primary key in it.
When any statement in batch has duplicate key, then it throws BatchUpdateException
(which is expected). But when there is a BatchUpdateException
, it fails all further statements in batch.
Suppose batch has 5 statements. Exception is at 3rd statement only. Then 4th and 5th statement also fails.
It is unlike Db2 LUW, which successfully executes all statements after that failed statement.
Example:
UQTEST table has first column primary. Code is adding key as 2 when value of i is 3 to make key duplicate for a statement.
PreparedStatement stmt = conn.prepareStatement("INSERT INTO LIBTEST.UQTEST VALUES(?, ?)");
for (int i = 1; i <=5; i++)
{
if (i ==3)
{
stmt.setInt(1, 2);
}
else
{
stmt.setInt(1, i);
}
stmt.setString(2, "Abc");
stmt.addBatch();
}
try
{
stmt.executeBatch();
}
catch(BatchUpdateException ex)
{
int[] updatecounts = ex.getUpdateCounts();
System.out.printlns(updatecounts);
}
After running this code, only 2 records inserted in table as:
1 Abc
2 Abc
I am using jt400.jar and JDBC driver used is :
com.ibm.as400.access.AS400JDBCDriver()
I want all those statements to be executed successfully which does not have any exception.