从数据库删除约会

时间:2018-06-20 18:44:30

标签: java mysql sql javafx

我正在尝试删除已创建的约会,该约会具有由用户插入的特定“ CNP”,当我搜索约会时,它会显示,但是当我尝试删除它时,什么也没有发生,我将粘贴控制器的完整代码,以便您更好地理解。

public class ProgramariDBController implements Initializable {

    @FXML
    private TextField cpn;
    @FXML
    private TableView<DetaliiProgramari> ProgrDB;
    @FXML
    private TableColumn<DetaliiProgramari, String> Nume;
    @FXML
    private TableColumn<DetaliiProgramari, String> Prenume;
    @FXML
    private TableColumn<DetaliiProgramari, String> Data;
    @FXML
    private TableColumn<DetaliiProgramari, String> Ora;
    @FXML
    private TableColumn<DetaliiProgramari, String> Departament;
    @FXML
    private TableColumn<DetaliiProgramari, String> Doctor;
    @FXML
    private TableColumn<DetaliiProgramari, String> Nr_telefon;

    public LogareController Numeutilzator = new LogareController();
    private ObservableList<DetaliiProgramari> Info;


    @Override
    public void initialize(URL url, ResourceBundle ResurcesFORDAYS) {
        // TODO

    }

    @FXML
    private void AfiseazaProgramari(ActionEvent event) {
        try {
          ConectaredB ConectaredB=new ConectaredB();
            Connection conectare=ConectaredB.logareDB();

            Info = FXCollections.observableArrayList();
            String CNP=cpn.getText();
            ResultSet IncDate = conectare.createStatement().executeQuery("SELECT *  FROM programari where CNP=" + CNP);
            while (IncDate.next()) {

                Info.add(new DetaliiProgramari(IncDate.getString(2), IncDate.getString(3), IncDate.getString(4),
                        IncDate.getString(5), IncDate.getString(6), IncDate.getString(7), IncDate.getString(8)));
            }

        } catch (SQLException ex) {
            System.err.println("Error"+ex);
        }


        Nume.setCellValueFactory(new PropertyValueFactory<>("Nume"));
        Prenume.setCellValueFactory(new PropertyValueFactory<>("Prenume"));
        Data.setCellValueFactory(new PropertyValueFactory<>("Data"));
        Ora.setCellValueFactory(new PropertyValueFactory<>("Ora"));
        Departament.setCellValueFactory(new PropertyValueFactory<>("Departament"));
        Doctor.setCellValueFactory(new PropertyValueFactory<>("Doctor"));
        Nr_telefon.setCellValueFactory(new PropertyValueFactory<>("Nr_telefon"));

        ProgrDB.setItems(null);
        ProgrDB.setItems(Info);
    }

    @FXML // Here is the method that I've wrote.
    private void delete(ActionEvent event) throws IOException  {
        try { 


            String sql = "delete FROM programari where CNP=?";
            ConectaredB ConectaredB=new ConectaredB();
            Connection conexiune=ConectaredB.logareDB();
            PreparedStatement ps = conexiune.prepareStatement(sql);
            String CNP=cpn.getText();
            ps.setString(1, CNP);
            ResultSet result = ps.executeQuery();

        } catch (Exception e) {
            // TODO: handle exception
        }
    }
}

1 个答案:

答案 0 :(得分:1)

SQL删除不会返回ResultSet,您需要调用executeUpdate()。改变这个

ResultSet result = ps.executeQuery();

ps.executeUpdate();

如果要验证结果,则可以告诉您结果去除了多少

int count = ps.executeUpdate();