我想对我的数据库执行简单的删除操作,但我的程序变得忙碌并且它停止响应。我不知道发生了什么,因为它没有显示错误。
private void deleteCustomer(ActionEvent event) {
String mName = name.getText();
String mID = id.getText();
Boolean flag = mName.isEmpty() || mID.isEmpty();
if(flag){
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setHeaderText(null);
alert.setContentText("Please Enter All Fields.");
alert.showAndWait();
return;
}
String st = "DELETE CUSTOMER WHERE id = '"+ mID +"'" ;
System.out.println(st);
if(handler.execAction(st)){
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setHeaderText(null);
alert.setContentText("Deleted");
alert.showAndWait();
//return;
}else{
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setHeaderText(null);
alert.setContentText("Error Occured");
alert.showAndWait();
}
}
这是我用来完成任务的方法
public boolean execUpdate(String query){
try {
stmt = conn.createStatement();
//stmt.executeUpdate(query);
if(stmt.executeUpdate(query) == 1){
// JOptionPane.showMessageDialog(null, "data " + message);
System.out.println("data deleted succefully!!");
}
return true;
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "error: "+ e.getMessage(), "error occoured", JOptionPane.ERROR_MESSAGE);
System.out.println("Exception at execQuery: dataHandler" + e.getLocalizedMessage());
return false;
}finally{
}
public boolean execAction(String query){
try {
stmt = conn.createStatement();
stmt.execute(query);
return true;
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "error: "+ e.getMessage(), "error occoured", JOptionPane.ERROR_MESSAGE);
System.out.println("Exception at execQuery: dataHandler" + e.getLocalizedMessage());
return false;
}finally{
}
}
答案 0 :(得分:2)
我想你忘了在SQL中添加FROM
关键字
String st = "DELETE FROM CUSTOMER WHERE id = '"+ mID +"'" ;