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?
executeUpdate() or execute() followed by getUpdateCount() will return the number of rows matched, not updated, according to the JDBC spec.
Get the Number of Rows Affected Using the execute() Method The execute() method executes the given SQL query, which may return multiple results.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With