Sobes.tech
Junior — Middle

In what cases is the executeUpdate method in JDBC recommended?

sobes.tech AI

Answer from AI

The executeUpdate method in JDBC is used to execute SQL queries that modify data in the database but do not return a result set. Typically, these are INSERT, UPDATE, DELETE queries, as well as DDL statements (CREATE TABLE, DROP TABLE, etc.).

The method returns the number of rows affected by the query, which helps verify the success of the operation.

Example usage:

String sql = "UPDATE employees SET salary = salary * 1.1 WHERE department = 'Sales'";
int rowsUpdated = statement.executeUpdate(sql);
System.out.println("Rows updated: " + rowsUpdated);