【发布时间】:2021-12-22 15:38:12
【问题描述】:
我有一个“addAppointment”和一个“updateAppointment”方法,它们都使用相同的参数。
public static void addAppointment(String title, String description, String location, String type, LocalDateTime start, LocalDateTime end, int customerID, int userID, int contactID)
{
try { String sqlAddAppt = "INSERT INTO appointments VALUES (NULL, ?, ?, ?, ?, ?, ?, NULL, NULL, NULL, NULL, ?, ?, ?)";
PreparedStatement ps = JDBC.getConnection().prepareStatement(sqlAddAppt);
ps.setString(1, title);
ps.setString(2, description);
ps.setString(3, location);
ps.setString(4, type);
ps.setTimestamp(5, Timestamp.valueOf(start));
ps.setTimestamp(6, Timestamp.valueOf(end));
ps.setInt(7, customerID);
ps.setInt(8, userID);
ps.setInt(9, contactID);
ps.execute();
}
catch (SQLException e) {
e.printStackTrace(); }}
public static void updateAppointment(String title, String description, String location, String type, LocalDateTime start, LocalDateTime end, int customerID, int userID, int contactID) {
try {
String sqlUpdate = "UPDATE appointments SET Title=?, Description=?, Location=?, Type=?, Start=?, End=?, Customer_ID=?, User_ID=?, Contact_ID=? WHERE Appointment_ID = ?;";
PreparedStatement ps = JDBC.getConnection().prepareStatement(sqlUpdate);
ps.setString(1, title);
ps.setString(2, description);
ps.setString(3, location);
ps.setString(4, type);
ps.setTimestamp(5, Timestamp.valueOf(start));
ps.setTimestamp(6, Timestamp.valueOf(end));
ps.setInt(7, customerID);
ps.setInt(8, userID);
ps.setInt(9, contactID);
ps.execute(); } catch (SQLException e) {
e.printStackTrace(); }}
我使用相同的方式调用方法:
DBAppointments.updateAppointment(title, description, location, type, startTimeUTC.toLocalDateTime(), endTimeUTC.toLocalDateTime(), customerID.getCustomerID(), userID.getUserID(), contact.getContactID());
DBAppointments.addAppointment(title, description, location, type, startTimeUTC.toLocalDateTime(), endTimeUTC.toLocalDateTime(), customerID.getCustomerID(), userID.getUserID(), contact.getContactID());
“addAppointment”工作完美,可以正确插入,但是当我尝试使用更新方法时,它不会返回任何错误,但也不会更新数据库。有什么想法吗?
【问题讨论】:
-
调用 updateAppointment 时正在处理什么?它运行了 exceucte() 吗?
-
Appointment_ID好像没有设置
-
检查事务是显式提交还是隐式提交。此外,使用显示的参数值启用 DEBUG 和 SQL 查询日志记录是有意义的。这将有助于进一步诊断问题。此外,您可以阅读数据库日志并查看数据库看到的内容
-
@HanhNguyen 谢谢你的回答!我从数据库中已经存在的约会中提取信息作为变量“selectedAppointment”。我使用“selectedAppointment.getAppointmentID”而不是“appointmentIDText”设置它,现在它可以正确更新。感谢您的推动!