Reputation: 21
I am writing a program in Java to insert a bunch of information to my MySQL DB, and need to track the number and reason of failures. I understand executeUpdate()
will return the number of rows that were affected by the SQL statement.
int rows = stmt.executeUpdate("INSERT/UPDATE...");
My question is, if the return value is 0 (no exception in this case), is there a way to find the possible reasons that cause the INSERT/UPDATE
to not work?
Upvotes: 2
Views: 771
Reputation: 192
Well, it did not fail. Zero stands for no change. I see two ways to get more information.
use stmt.execute() which returns false then check the integer value. if it is -1, then there was result at all which is a bad sign.
Use Result set for the insert/update/delete operations. But you need to retrieve the exact data you would like to update/.....
Note: Had an exception been thrown, you would have gotten a more friendly message in the catch block, exc.getMessage()
Upvotes: 1
Reputation: 94
For UPDATE, a return value of 0 is obvious: no rows updated. For INSERT, the only situation I can think of is when you try to perform an INSERT SELECT (...) and the result of the SELECT contains 0 rows.
Upvotes: 2