views:

352

answers:

3

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?

+1  A: 

That number is returned when you run the query:

int rows = prep1.executeUpdate(); 
System.out.printf("%d row(s) updated!", rows); 
Andomar
+2  A: 

Calling executeUpdate() on your PreparedStatement should return an int, the number of updated records.

Brabster
Lovely :) Thanks guys
Krt_Malta
A: 

If it is necessary to know how many rows will be affected without executing it, you will have to run a SELECT statement first.

Michael Munsey