java - 更新查询不更新数据库?
问题描述
我有一个“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”工作完美,可以正确插入,但是当我尝试使用更新方法时,它不会返回任何错误,但也不会更新数据库。有任何想法吗?
解决方案
您可以使用 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);
}
}
您可以在此处阅读更多信息
推荐阅读
- css - 制作一个不可点击的菜单项
- java - 验证 REST API 中的查询参数
- wordpress - Gutenberg 块 - 在块内处理服务器数据
- java - 如何处理“在 SpEL 中的对象上找不到属性或字段”?
- javascript - Leaflet circleMarker 弹出窗口现在显示
- email - Gitlab不发送电子邮件
- java - Vaadin 8 以编程方式更改全局字体
- jquery - css jquery动画与文本之间的延迟
- javascript - 测量卷轴最准确的方法是什么?
- angular - dotnet 核心中的 Angular 应用程序。页面不会更新正在更改的文件的更改