从表视图和sql中删除数据

问题描述 投票:-1回答:1

我想从表视图和SQL中删除数据,在我尝试了几乎所有我不知道问题出在哪里之后?

public void DeleteButton(ActionEvent event) throws SQLException, 
    ClassNotFoundException{

   String sql = "Delete from Add_NewOrder where No=?";
   try{
       pst = con.prepareStatement(sql);
       pst.setString(1, comboBoxTable.getValue());
       int i = pst.executeUpdate();
       if(i==1){
            Alert alert = new Alert(AlertType.ERROR);
            alert.setTitle("Information Dialog");
            alert.setHeaderText(null);
            alert.setContentText("Te dhenat nuk jane shlyer!");
            alert.showAndWait();
            loadDataFromDataBase();
            clearTextField();
       }
   }catch(SQLException ex){
        Logger.getLogger(AddNewOrderController.class.getName()).log(Level.SEVERE,null,ex);
   }
}
java database javafx database-connection
1个回答
1
投票

哪部分不起作用?

我发现你的代码有很多问题:

  1. Connection和PreparedStatement似乎是类变量。我将PreparedStatement保留在方法范围中,并在finally块中关闭它。
  2. 方法是做两件事:数据库和Swing UI更改。将它们分成单独的类和方法。单独测试它们并在它们都工作时将它们组合。
  3. 我试图避免在同一个类中混合UI和处理代码。我将它们分成不同的类。
© www.soinside.com 2019 - 2024. All rights reserved.