更新查詢不更新數據庫?

丟失的代碼鍵

我有一個“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”工作完美,可以正確插入,但是當我嘗試使用更新方法時,它不會返回任何錯誤,但也不會更新數據庫。有任何想法嗎?

Nthabiseng Mashiane

您可以使用 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);
      }

  
}

您可以在此處閱讀更多相關信息

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章