JavaFx, Inserting data into MySQL Database

in java •  6 years ago 

DeepinScreenshot_iksan.Iksan_20180904142907.png

@FXML
private void simpan(ActionEvent event) {
simpan();
}

private void simpan() {
    if (txtid.getText().isEmpty() | txtnama.getText().isEmpty()) {
        TrayNotification tnn = new TrayNotification("WARNING", "ID Kampung atau Nama Kampung \nTidak Boleh Kosong", NotificationType.WARNING);
        tnn.setAnimationType(AnimationType.FADE);
        tnn.showAndDismiss(Duration.seconds(2));

    } else if (panjang(txtid.getText(), panjanginput)) {
        TrayNotification tnn = new TrayNotification("WARNING", "ID Tidak Boleh \nLebih Dari Lima (5) Karakter", NotificationType.WARNING);
        tnn.setAnimationType(AnimationType.FADE);
        tnn.showAndDismiss(Duration.seconds(2));
    } else {
        try {
            Statement stt = conn.createStatement();
            String sql = "select * from kampung where id_kampung = '" + txtid.getText() + "'";
            ResultSet rs = stt.executeQuery(sql);
            if (rs.next() == true) {
                TrayNotification tn = new TrayNotification("WARNING", "ID Kampung Sudah Terdaftar", NotificationType.NOTICE);
                tn.setAnimationType(AnimationType.FADE);
                tn.showAndDismiss(Duration.seconds(1));
                refresh();
            } else {
                conn = konek.konekDB();
                String insert = "insert into kampung(id_kampung,nm_kampung) values (?,?)";
                try {
                    pst = conn.prepareStatement(insert);
                    pst.setString(1, txtid.getText().toUpperCase());
                    pst.setString(2, txtnama.getText().toUpperCase());
                    int berhasil = pst.executeUpdate();
                    if (berhasil == 1) {
                        hapus();
                        refresh();
                        TrayNotification tn = new TrayNotification("SUCCESS", "Data Berhasil Berhasil", NotificationType.SUCCESS);
                        tn.setAnimationType(AnimationType.FADE);
                        tn.showAndDismiss(Duration.seconds(2));
                    } else {
                        TrayNotification tn = new TrayNotification("ERROR", "Data Berhasil GAGAL", NotificationType.ERROR);
                        tn.setAnimationType(AnimationType.FADE);
                        tn.showAndDismiss(Duration.seconds(2));
                    }

                } catch (SQLException ex) {
                    System.err.println("Error inserting" + ex);
                    TrayNotification tn = new TrayNotification("WARNING", "Periksa Kembali Inputan Anda", NotificationType.WARNING);
                    tn.setAnimationType(AnimationType.FADE);
                    tn.showAndDismiss(Duration.seconds(2));
                }
            }

        } catch (SQLException ex) {
            System.err.println("Error inserting" + ex);
            TrayNotification tn = new TrayNotification("WARNING", "Periksa Kembali Inputan Anda", NotificationType.WARNING);
            tn.setAnimationType(AnimationType.FADE);
            tn.showAndDismiss(Duration.seconds(2));
        }
    }

}
Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!