我正在使用mySQL数据库的学校项目中工作。我现在正在处理的特定部分是系统的用户尝试从数据库中删除约会时发生的情况。我有三个警报,一个警报起作用,一个警报出现两次,而另一个警报根本没有起作用。第一个循环,即验证选择的循环确实起作用。第二个确认用户是否要删除的消息将运行两次,即,当您单击“确定”按钮时,它将再次显示,您必须再次单击。然后,似乎跳到我重新加载页面的底部。重新加载页面后,我可以看到约会已成功删除,并且在mysql工作台中也显示为已删除。因此,只是中间警报似乎根本没有运行。我搜寻了互联网,以找出为什么其中一个显示两次,而另一个则根本不显示,尽管我发现了类似的问题和尝试使用其解决方案的问题,但没有发现任何区别。感谢您在正确的方向上提供的任何帮助,无论是代码更正还是资源!提前非常感谢您。
// delete selected appointment
@FXML
void handleDelete(MouseEvent event) throws IOException, SQLException {
Appointment ifSelected = appointmentTable.getSelectionModel().getSelectedItem();
if (ifSelected == null){
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle("Deletion Error");
alert.setHeaderText("You didn't choose an appointment to delete.");
alert.setContentText("Please click ok in order to choose an appointment for deletion.");
alert.showAndWait();
}
else{
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Appointment Deletion");
alert.setHeaderText("You are about to delete an appointment record permanantly.");
alert.setContentText("If you want to proceed, click ok.");
Optional<ButtonType> result = alert.showAndWait();
if (result.isPresent() && result.get() == ButtonType.OK){
Alert alert2 = new Alert(Alert.AlertType.INFORMATION);
alert2.setTitle("Deletion Successful");
alert2.setHeaderText("You successfully deleted the appointment with " + ifSelected.getCustomerName()+ " at " + ifSelected.getStartTime() + ".");
alert.setContentText("If you want to proceed, click ok.");
alert.show();
Statement stmt = conn.createStatement();
apptID = ifSelected.getAppointmentID();
String sqlDeleteAppointment = "DELETE FROM appointment WHERE appointmentId = " + apptID;
Query.makeQuery(sqlDeleteAppointment);
Parent root = FXMLLoader.load(getClass().getResource("appointmentScreen.fxml"));
scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
}}
'''
您已经从Alert类为嵌套的Alerts创建了alert2对象。但是您使用的是alert对象而不是alert2。
Alert alert2 = new Alert(Alert.AlertType.INFORMATION);
alert2.setTitle("Deletion Successful");
alert2.setHeaderText("You successfully deleted the appointment with " + ifSelected.getCustomerName()+ " at " + ifSelected.getStartTime() + ".");
alert2.setContentText("If you want to proceed, click ok.");
alert2.show();