pstmt.executeUpdate()
时间: 2024-05-08 22:15:15 浏览: 76
新手教程之:Java操作Sql_server
The method `executeUpdate()` is a method of the `PreparedStatement` interface in Java. It is used to execute an SQL statement that modifies the database, such as an INSERT, UPDATE or DELETE statement.
This method returns an integer value indicating the number of rows affected by the statement. It can also throw an `SQLException` if there is an error executing the statement.
Example usage:
```
String sql = "UPDATE students SET grade= 'A' WHERE id = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, 123);
int rowsAffected = pstmt.executeUpdate();
System.out.println(rowsAffected + " rows updated.");
```
In this example, we create a prepared statement to update the grade of a student with ID 123 to 'A'. We then execute the statement using `executeUpdate()` and print the number of rows affected.
阅读全文