简体   繁体   English

从表视图和sql中删除数据

[英]Deleting data from table view and from sql

I want to delete the data from table view and from SQL,after i tried almost everything i don't know where the problem is?我想从表视图和 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);
   }
}

Which part isn't working?哪个部分不起作用?

I see lots of problems with your code:我看到你的代码有很多问题:

  1. Connection and PreparedStatement appear to be class variables. Connection 和 PreparedStatement 似乎是类变量。 I'd keep PreparedStatement in method scope and close it in a finally block.我将 PreparedStatement 保留在方法范围内并在 finally 块中关闭它。
  2. Method is doing two things: database and Swing UI change.方法是做两件事:数据库和Swing UI 更改。 Separate them into individual classes and methods.将它们分成单独的类和方法。 Test them separately and combine them when both are working.分别测试它们并在两者都工作时将它们组合起来。
  3. Mixing UI and processing code in the same class is something I try to avoid.我尽量避免在同一个类中混合 UI 和处理代码。 I'd partition them into separate classes.我会将它们分成不同的类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM