Reputation: 61
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();
}
}
Upvotes: 1
Views: 8783
Reputation: 381
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);
Upvotes: 3
Reputation: 455
maybe you are using it wrong:
int[] executeBatch() throws SQLException
Submits a batch of commands to the database for execution and if all commands execute successfully, returns an array of update counts. The int elements of the array that is returned are ordered to correspond to the commands in the batch, which are ordered according to the order in which they were added to the batch. The elements in the array returned by the method executeBatch may be one of the following:
1)A number greater than or equal to zero -- indicates that the command was processed successfully and is an update count giving the number of rows in the database that were affected by the command's execution
2)A value of SUCCESS_NO_INFO -- indicates that the command was processed successfully but that the number of rows affected is unknown If one of the commands in a batch update fails to execute properly, this method throws a BatchUpdateException, and a JDBC driver may or may not continue to process the remaining commands in the batch. However, the driver's behavior must be consistent with a particular DBMS, either always continuing to process commands or never continuing to process commands. If the driver continues processing after a failure, the array returned by the method BatchUpdateException.getUpdateCounts will contain as many elements as there are commands in the batch, and at least one of the elements will be the following:
3)A value of EXECUTE_FAILED -- indicates that the command failed to execute successfully and occurs only if a driver continues to process commands after a command fails
Upvotes: 0