Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return number of rows affected by SQL UPDATE statement in Java

I'm using a MySQL database and accessing it through Java.

PreparedStatement prep1 = this.connection.prepareStatement(         "UPDATE user_table          SET Level = 'Super'          WHERE Username = ?"); prep1.setString(1, username); 

The update statement above works fine however I'd like to get the number of rows affected with this statement. Is this possible please?

like image 720
Krt_Malta Avatar asked Apr 03 '10 16:04

Krt_Malta


People also ask

How do I get the number of rows affected in a JDBC insert statement?

executeUpdate() or execute() followed by getUpdateCount() will return the number of rows matched, not updated, according to the JDBC spec.

Which method returns the number of rows affected by the execution of the SQL statement?

Get the Number of Rows Affected Using the execute() Method The execute() method executes the given SQL query, which may return multiple results.

Does SQL update return rows?

The optional RETURNING clause causes UPDATE to compute and return value(s) based on each row actually updated. Any expression using the table's columns, and/or columns of other tables mentioned in FROM , can be computed. The new (post-update) values of the table's columns are used.


1 Answers

Statement.executeUpdate() or execute() followed by getUpdateCount() will return the number of rows matched, not updated, according to the JDBC spec. If you want the updated count, you can specify useAffectedRows=true as a non-standard URL option. More information is available here.

like image 164
Trevor Robinson Avatar answered Sep 19 '22 20:09

Trevor Robinson