从数据库删除记录时

时间:2018-11-16 13:30:46

标签: java javafx

您好,我尝试验证记录是否存在于此方法中时出现问题,警报对话框正常,屏幕上显示“ registro no encontrado”,但是当我编写正确的记录以删除时却没有出现警报对话框“ registro borradocorrectamente”,但是它确实删除了记录,任何想法我可能做错了什么,问候。

这是我的代码。

public void borraregistro() {

             Integer numfac=null;
             String consulta=" delete  from auditoriac where numero_factura=? ";

             Connection conn=null;{

                  try {
                      try {
                             numfac = Integer.parseInt(borrar.getText());
                         }catch (NumberFormatException ex) {
                             Alert alert = new Alert(AlertType.INFORMATION);
                              alert.setTitle("Informacion");
                              alert.setHeaderText(null);
                              alert.setContentText("Campo Vacio, Por favor Digite el numero de Factura:" +ex);
                              alert.getDialogPane().setStyle("-fx-text-fill: white;\r\n" + 
                                        "    -fx-border-color:  rgb(238, 201, 91);\r\n" + 
                                        "    -fx-border-radius: 5;\r\n" + 
                                        "    -fx-padding: 10 2 10 -2;\r\n" + 
                                        "    -fx-background-color:linear-gradient(to bottom, #ffffcc 15%, #ffcc99 91%);\r\n" + 
                                        "    -fx-text-fill:black;\r\n" + 
                                        "    -fx-font-family: Oswald;\r\n" + 
                                        "    -fx-font-size:15px;    ");
                              alert.showAndWait();      



                         }
                      conn=DriverManager.getConnection("jdbc:sqlserver://DESKTOP-4JA6SFR:1433;databaseName=GLOSASNINO", "sa", "123");
                      PreparedStatement ps =conn.prepareStatement(consulta);
                      ps.setInt(1, numfac);
                      ps.executeUpdate(); 
                      int comparar =ps.executeUpdate(); 
                     if (comparar==0){
                            Alert alerta = new Alert(AlertType.INFORMATION);
                              alerta.setTitle("Informacion");
                              alerta.setHeaderText(null);
                              alerta.setContentText("Registro no encontrado");
                              alerta.getDialogPane().setStyle("-fx-text-fill: white;\r\n" + 
                                        "    -fx-border-color:  rgb(238, 201, 91);\r\n" + 
                                        "    -fx-border-radius: 5;\r\n" + 
                                        "    -fx-padding: 10 2 10 -2;\r\n" + 
                                        "    -fx-background-color:linear-gradient(to bottom, #ffffcc 15%, #ffcc99 91%);\r\n" + 
                                        "    -fx-text-fill:black;\r\n" + 
                                        "    -fx-font-family: Oswald;\r\n" + 
                                        "    -fx-font-size:15px;    ");
                              alerta.showAndWait();  

                         }


                     else {  
                              Alert alerta = new Alert(AlertType.INFORMATION);
                              alerta.setTitle("Informacion");
                              alerta.setHeaderText(null);
                              alerta.setContentText("Registro borrado correctamente");
                              alerta.getDialogPane().setStyle("-fx-text-fill: white;\r\n" + 
                                        "    -fx-border-color:  rgb(238, 201, 91);\r\n" + 
                                        "    -fx-border-radius: 5;\r\n" + 
                                        "    -fx-padding: 10 2 10 -2;\r\n" + 
                                        "    -fx-background-color:linear-gradient(to bottom, #ffffcc 15%, #ffcc99 91%);\r\n" + 
                                        "    -fx-text-fill:black;\r\n" + 
                                        "    -fx-font-family: Oswald;\r\n" + 
                                        "    -fx-font-size:15px;    ");
                              alerta.showAndWait();  

                             }



                  }catch (SQLException e) {

                  }

             }

             seleccionaregistros();
        } 

1 个答案:

答案 0 :(得分:2)

您执行两次更新:

ps.executeUpdate(); 
int comparar = ps.executeUpdate();

第一次调用executeUpdate()方法时,记录将被删除。在下一行,当您再次调用该方法并设置comparar变量时,没有什么可删除的,因此0行受到影响。

删除第一个ps.executeUpdate(),您应该会看到不同的结果。