【问题标题】:Update query isn't updating the database?更新查询不更新数据库?
【发布时间】: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”设置它,现在它可以正确更新。感谢您的推动!

标签: java sql


【解决方案1】:

您可以使用 Hibernate 和 JDBC,这是在您的数据库上执行 CRUD 操作的一种更简洁的方式。如果您以前没有见过它可能是一种完全不同的方法,但是我建议您熟悉这种方法,因为它是当前执行数据库操作的标准方法。

首先定义你的实体和接口,然后添加一个控制器来执行你的操作

@Entity('TableName')
public class Person {
 @Column('table_column_name')
 String title;

 @Column('table_column_name')
 String description;

 @Column('table_column_name')
 String location;

 @Column('table_column_name')
 String type; 

 @Column('table_column_name')
 LocalDateTime start; 

 @Column('table_column_name')
 LocalDateTime end; 

 @Column('table_column_name')
 int customerID; 

 @Column('table_column_name')
 int userID; 

 @Column('table_column_name')
 int contactID;
}

This person class is basically the column headings of your table in your 
database

您将创建一个包含数据库操作的接口

    public interface PersonRepository extends CrudRepository<Person, Long> {
      /*This will contain all the database operations you want to perform. By 
       default, it contains the CRUD operations and if you will be performing 
       CRUD operations only, you don't need to add anything*/
    }

You will then have a controller where you will perform your crud operations

@RestController
public class Controller {
  @Autowired
  PersonRepository personRepo;

  @PostMapping("/save")
  public void addAppointment(Person person) {
    personRepo.save(person);
  }

  @PutMapping("/update")
  public void updateAppointment(int personID, String description, int customerID) {
      //retrieve the user whose appointment you want to update then update the relevant fields and save user;
        Person person = personRepo.findById(personID);
        person.setDescription(description);
        person.setCustomerID(customerID);
        personRepo.save(person);
      }

  
}

你可以阅读更多关于这个here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-20
    • 1970-01-01
    • 2020-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多