deejay
deejay

Reputation: 575

JDBC - Oracle - BatchUpdateException.getUpdateCounts() returns blank array

I am trying to execute the batch query using PreparedStatement.executeBatch() and trying to catch any exception occurred in BatchUpdateException. But I am getting a blank array on calling BatchUpdateException.getUpdateCounts().

Below is my code:

int[] updateCounts = null;  
PreparedStatement stmt = null;  
// some logic to set the stmt and create the SQL query
try {  
if (count % 100 == 0)   
    {  
        updateCounts = stmt.executeBatch();  
     }  
} catch (BatchUpdateException bu)  
{   
        updateCounts = bu.getUpdateCounts();  
}

Here I get an empty array of updateCounts when an exception occurs... Why?

NOTE:

BatchUpdateException -

SystemErr R java.sql.BatchUpdateException: ORA-01400: cannot insert NULL into ("TABLE_ABC"."AMOUNT")

CSV file from where the code reads the data... expecting error in the 3rd record

Asof_Date,Price_Date,Amount
12/15/2015,11/26/2014,-2646.937686
12/15/2015,11/28/2014,5053.611471
12/15/2015,1/22/2015,
12/15/2015,1/23/2015,

Upvotes: 1

Views: 2378

Answers (1)

Jan
Jan

Reputation: 13858

Well, according to the javadoc, you get one int for the update count of every successful executed statement so an empty array suggests failure on first statement in batch.

Retrieves the update count for each update statement in the batch update that executed successfully before this exception occurred.

As you are able to tell from your Exception, empty values in your CSV cause the failure. So you either allow NULL for that column or check if null and insert 0 instead.

As for the updateCounts: if your batch ran in one transaction (which would make sense) then all inserts would be rolled nack on error - resulting in nothing done.

Upvotes: 1

Related Questions