executeUpdate
时间: 2024-05-14 12:15:08 浏览: 101
executeUpdate is a method in Java that is used to execute an SQL statement that modifies data in a database. It is commonly used to execute SQL statements such as INSERT, UPDATE, and DELETE.
The executeUpdate method returns an integer value that represents the number of rows that were affected by the SQL statement. If the SQL statement was executed successfully, the return value will be greater than or equal to zero. If an error occurred during the execution of the SQL statement, the method will throw an exception.
Here is an example of using executeUpdate to execute an SQL statement:
```
try {
Statement stmt = conn.createStatement();
String sql = "UPDATE customers SET age = 25 WHERE id = 1";
int rowsUpdated = stmt.executeUpdate(sql);
System.out.println("Rows updated: " + rowsUpdated);
} catch (SQLException e) {
System.out.println("Error executing SQL statement: " + e.getMessage());
}
```
In this example, the SQL statement updates the age of a customer with the ID of 1 to 25. The executeUpdate method is used to execute the SQL statement, and the number of rows that were updated is printed to the console. If an error occurs during the execution of the SQL statement, the catch block will handle the exception and print an error message.
阅读全文