samedi 5 juin 2021

How to delete multiple records from a sqlite database via checkboxes in javafx tableview?

I am trying to build a new function for my program a bulk delete option so the user can delete multiple records at once via checkboxes from the tableview and database.

I have the table view side working but when I try to do the sql side I can not seem to properly write the sql so that the records that have the checkboxes checked are deleted from the database and on top of that I keep getting Concurrent modification exception(only when I try to run the sql and tableview code together).

I have tried putting the observable list of objects to be deleted in the string part of the prepared statement using the .value of but that has not worked and I also tried putting the pack I had to make for the for loop in that did not work, and I tried to use the get Select method (Checkbox) but got some sort of static reference error. I am unfortinately out of ideas.

Here is the code for the object, tableview, Bulk Delte method , and the stacktrace for the concurent error message.

Pack Object

public class Pack  {

    private int PackId;
    private String PackName;
    private String VendorName;
    private float PackValue;
private CheckBox select;

    public Pack(){

        this.PackId = 0;
        this.PackName = "" ;
        this.VendorName = "" ;
        this.PackValue = 0;


    }


    public Pack(  int PackId, String PackName, String VendorName, float PackValue) {

        this.PackId = PackId;
        this.PackName = PackName;
        this.VendorName = VendorName;
        this.PackValue = PackValue;
        this.select = new CheckBox();



    }





    public  int getPackId() {
        return PackId;
    }

    public void setPackId(int packId) {
        PackId = packId;
    }

    public String getPackName() {
        return PackName;
    }

    public void setPackName(String packName) {
        PackName = packName;
    }

    public String getVendorName() {
        return VendorName;
    }

    public void setVendorName(String vendorName) {
        VendorName = vendorName;
    }

    public float getPackValue() {
        return PackValue;
    }

    public void setPackValue(float packValue) {
        PackValue = packValue;
    }

    public CheckBox getSelect() {
        return select;
    }

    public void setSelect(CheckBox select) {
        this.select = select;
    }
}

Tableview

table = new TableView<>();
        PackId = new TableColumn<>("PackId");
        PackId.setPrefWidth(100);
        PackId.setCellValueFactory(new PropertyValueFactory<>("PackId"));
        PackName = new TableColumn<>("PackName");
        PackName.setPrefWidth(200);
        PackName.setCellValueFactory(new PropertyValueFactory<>("PackName"));
        VendorName = new TableColumn<>("VendorName");
        VendorName.setPrefWidth(200);
        VendorName.setCellValueFactory(new PropertyValueFactory<>("VendorName"));
        PackValue = new TableColumn<>("PackValue");
        PackValue.setPrefWidth(200);
        PackValue.setCellValueFactory(new PropertyValueFactory<>("PackValue"));
        selectColumn = new TableColumn<>("Select");
        selectColumn.setCellValueFactory(new PropertyValueFactory<>("select"));
        selectColumn.setPrefWidth(98);
        table.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
        table.getColumns().add(PackId);
        table.getColumns().add(PackName);
        table.getColumns().add(VendorName);
        table.getColumns().add(PackValue);
        table.getColumns().add(selectColumn);
        table.relocate(60 , 180);
        table.setPrefSize(800 , 250);
        getPacks();
        table.setOnMouseClicked(mouseEvent -> {
            Pack pack = table.getSelectionModel().getSelectedItem();
            packId.setText(String.valueOf(pack.getPackId()));
            packName2.setText(pack.getPackName());
            vendorName.setText(pack.getVendorName());
            packValue.setText(String.valueOf(pack.getPackValue()));
        });

Bulk Delete Method

public void bulkDelete(ActionEvent event){

        ObservableList<Pack>packListRemove = FXCollections.observableArrayList();
        for (Pack bean : packList)
        {
            if (bean.getSelect().isSelected())
            {
               packListRemove.add(bean);
            }

            packList.removeAll(packListRemove);
        }

                try {
                        Connection conn = DriverManager.getConnection("jdbc:sqlite:packs.db");
                        String sql = "delete from packs Where PackId = ?";
                        PreparedStatement ps;
                        ps = conn.prepareStatement(sql);
                        ps.setString(1,packId.getText());
                        ps.executeUpdate();
                        ps.addBatch();
                        System.out.println("Data has been Deleted");

                } catch (Exception e) {
                        e.printStackTrace();

                }
                refreshPacks();

        }

Stack trace

Exception in thread "JavaFX Application Thread" java.util.ConcurrentModificationException
    at java.base/java.util.AbstractList$Itr.checkForComodification(AbstractList.java:399)
    at java.base/java.util.AbstractList$Itr.next(AbstractList.java:368)
    at Main.bulkDelete(Main.java:604)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
    at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8889)
    at javafx.controls/javafx.scene.control.Button.fire(Button.java:203)
    at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:208)
    at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
    at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3856)
    at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1851)
    at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2584)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:409)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:299)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:447)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:412)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:446)
    at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
    at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    at java.base/java.lang.Thread.run(Thread.java:831)

Thank you for your time and any help

Thomas Gustafson

p.s. Sorry about the amount of code.




Aucun commentaire:

Enregistrer un commentaire