从 table 视图和 sql 中删除数据
Deleting data from table view and from sql
我想从 table 视图和 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);
}
}
哪个部分不工作?
我发现你的代码有很多问题:
- Connection 和 PreparedStatement 似乎是 class 变量。我会将 PreparedStatement 保留在方法范围内,并在 finally 块中将其关闭。
- 方法是做两件事:数据库和Swing UI改变。将它们分成单独的 classes 和方法。分别测试它们,当它们都起作用时将它们组合起来。
- 在同一个 class 中混合 UI 和处理代码是我尽量避免的事情。我会将它们分成单独的 classes.
我想从 table 视图和 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);
}
}
哪个部分不工作?
我发现你的代码有很多问题:
- Connection 和 PreparedStatement 似乎是 class 变量。我会将 PreparedStatement 保留在方法范围内,并在 finally 块中将其关闭。
- 方法是做两件事:数据库和Swing UI改变。将它们分成单独的 classes 和方法。分别测试它们,当它们都起作用时将它们组合起来。
- 在同一个 class 中混合 UI 和处理代码是我尽量避免的事情。我会将它们分成单独的 classes.