public boolean updateCustomer(Customer customer) { // 更新客户信息的具体实现 // ... return true; }补全代码
时间: 2024-02-29 07:55:25 浏览: 65
public boolean updateCustomer(Customer customer) {
// 更新客户信息的具体实现
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = getConnection(); // 获取数据库连接
String sql = "UPDATE customers SET name = ?, address = ? WHERE id = ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, customer.getName());
stmt.setString(2, customer.getAddress());
stmt.setInt(3, customer.getId());
int rows = stmt.executeUpdate();
if (rows > 0) {
return true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭资源
close(conn, stmt, null);
}
return false;
}
// 获取数据库连接
private Connection getConnection() throws SQLException {
// ...
}
// 关闭资源
private void close(Connection conn, Statement stmt, ResultSet rs) {
// ...
}
阅读全文