user3806290
user3806290

Reputation: 21

Failing reasons for executeUpdate("INSERT...");

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

Answers (2)

Xenwar
Xenwar

Reputation: 192

Well, it did not fail. Zero stands for no change. I see two ways to get more information.

  1. 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.

  2. 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

Cort-X7
Cort-X7

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

Related Questions