1

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,
3
  • By empty array, do you mean an array with length = 0? If so, then it's likely because you didn't actually add anything to the batch, e.g. count is 0. Commented Jan 5, 2016 at 17:21
  • Well printing the exception might give some insight Commented Jan 5, 2016 at 17:22
  • @Andreas - it has 11 records i.e count is 11 Commented Jan 5, 2016 at 18:24

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

According to Javadoc... "a BatchUpdateException provides the update counts for all commands that were executed successfully during the batch update, that is, all commands that were executed before the error occurred", I don't even get that too
Print your exception with bu.printStackTrace () and post it into your question
Updated answer to reflect your new info.
Is there any way to do the batch insert/update and to know the individual query success or failure?
can you share the code (edit question) for the actual statement? How do you fill in values and so on?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.