JDBC: Find out if query was successful?

java, jdbc, mysql

Solution

executeUpdate() will return the number of rows that were affected by your SQL statement:

int rows = stmt.executeUpdate("UPDATE ...");
System.out.println(rows + " rows updated");

Of course you could have found out yourself by simply looking at the JavaDocs:

Returns: either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements that return nothing

Problem

I'm using JDBC with mysql. I can get my queries to work great. But for update queries for instance I was wondering if there was any way to determine whether the update was successful for for example if the row could not be found. ``` UPDATE TABLE SET column = 'newvalue' WHERE primary_key =2 ``` I would like to get the specific error message if possible, this way I can throw a specific exception as to why the query failed. Thanks for your help.

Original source