How to Retrieve the Number of Rows Affected by an SQL UPDATE Statement in Java

Question

How can I return the number of rows affected by an SQL UPDATE statement in Java?

int rowsAffected = statement.executeUpdate(sqlQuery);

Answer

In Java, when executing an SQL UPDATE statement using JDBC, you can retrieve the number of rows affected by that operation. This is helpful for validating whether your database changes were successful.

Connection conn = DriverManager.getConnection(dbUrl, username, password);
String sqlQuery = "UPDATE employees SET salary = salary * 1.10 WHERE department = 'Sales'";
try (Statement statement = conn.createStatement()) {
    int rowsAffected = statement.executeUpdate(sqlQuery);
    System.out.println("Rows affected: " + rowsAffected);
} catch (SQLException e) {
    e.printStackTrace();
}

Causes

  • The statement was executed correctly against the database.
  • Permissions allow updating the intended rows.

Solutions

  • Utilize the `executeUpdate` method of the `Statement` or `PreparedStatement` object.
  • Assign the return value of `executeUpdate` to an integer to capture the number of affected rows.

Common Mistakes

Mistake: Not using `PreparedStatement` for dynamically built queries, leading to SQL injection risks.

Solution: Always prefer using `PreparedStatement` to safeguard against SQL injection.

Mistake: Assuming rows will be affected without proper conditions in the WHERE clause.

Solution: Double-check your SQL query conditions to ensure they are correct.

Helpers

  • Java SQL UPDATE rows affected
  • Java JDBC
  • executeUpdate method
  • SQL update example in Java

Related Questions

⦿How to Determine the File Extension from a URI

Learn how to extract the file extension from a URI in various programming languages. Stepbystep guide and code examples included.

⦿How to Handle RequestQueue Timeouts in Volley?

Learn how to effectively manage RequestQueue timeouts in Volley with best practices and code examples.

⦿How to Split a List into Sublists Based on a Condition Using the Stream API in Java?

Learn to efficiently split a list into sublists based on specific conditions using Javas Stream API with detailed examples and explanations.

⦿How to Restrict JFileChooser to Only Accept .txt Files

Learn how to configure JFileChooser in Java to restrict file selection to only .txt files using FileNameExtensionFilter.

⦿How to Format Dates in 24-Hour Time Using SimpleDateFormat in Java

Learn how to use SimpleDateFormat to format dates in Java with a 24hour time format. Stepbystep guide and common mistakes.

⦿How to Fix ImageView Not Displaying Images in Android?

Learn how to troubleshoot and resolve issues with ImageView not displaying images in Android applications.

⦿Why Isn't @RestController from a Different Package Working in My Spring Application?

Explore common issues with RestController in different packages in Spring applications and learn how to resolve them.

⦿How to Change the Color of the DatePicker Divider in Android?

Learn how to customize the DatePicker divider color in Android with stepbystep instructions and code examples.

⦿Does C# Have an Equivalent to the Java Runnable Interface?

Explore the similarities and differences between Javas Runnable interface and C threading mechanisms.

⦿How to Read a File from AWS S3 Using Java?

Learn how to read files from AWS S3 using Java with our stepbystep guide including example code and common troubleshooting tips.

© Copyright 2025 - CodingTechRoom.com