Am Johnny Bhai Am Johnny Bhai - 12 days ago 5
Java Question

Statement executeBatch method returns array of value 2

When I am executing statement.executeBatch() method in java, its returning an int array with value as 2

The jdbc-spec has the following to say about the return-code of batch-updates:

■ 0 or greater

■ Statement.SUCCESS_NO_INFO

■ -2

But i was getting everytime as 2.
The records are getting deleted successfully from database and it deletes 25 records from both the tables which satisfies the query . But in sysout it shows affected Records value as 2

Connection conn = null;
Statement stmt = null;
try {
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(CONNECTION_URL, USERNAME, PASSWORD);
stmt = conn.createStatement();
System.out.println("trying to execute query");

stmt.addBatch("DELETE FROM TABLE1 WHERE CRTE_TSTP < TRUNC(SYSDATE) - 14");
stmt.addBatch("DELETE FROM TABLE2 WHERE RECV_TSTP < TRUNC(SYSDATE) - 14");

int affectedRecords[] = stmt.executeBatch();
conn.commit();

System.out.println("number of rows deleted " + affectedRecords.length);

} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}


}

Answer

You are getting the length of the array, not the actual int values. Have you tried calculating the sum of the deletes from the elements of the array returned. For example:

int affectedRecordsSum = affectedRecords[0] + affectedRecords[1];
System.out.println("number of rows deleted  " + affectedRecordsSum);