String sql = "UPDATE students SET name=?, age=? WHERE id=?"; PreparedStatement statement = connection.prepareStatement(sql); statement.setString(1, "John"); statement.setInt(2, 22); statement.setInt(3, 1); int rowsAffected = statement.executeUpdate();
String sql = "DELETE FROM products WHERE price < ?"; PreparedStatement statement = connection.prepareStatement(sql); statement.setDouble(1, 10.0); int rowsAffected = statement.executeUpdate();In this example, we are deleting all the products whose price is less than 10. The SQL query is parameterized, and we are setting the value of the parameter using the setDouble method. After executing the query, we get the number of affected rows. Package library: java.sql Overall, java.util.PreparedStatement executeUpdate is a useful method for executing SQL statements that modify the database, and it is part of the java.sql package library.