how to push javafx tableview data into sqlite database?

37 Views Asked by At

I'm working on a store management app. Now I want to display all new products in a TableView before pushing it into database. To add new products to TableView I'm using a List<Product> and I can see all new products to a TableView but if I try to push them to the database with addbatch(), and executeBatch() method, I'm getting error sqlite file lock database. I have set autocommit to false. Here is my code:

public void validerFacture() throws SQLException {
    String ajoutProduits = BaseDeDonne.AJOUTER_PRODUITS;
    Connection connection = null;
    PreparedStatement ps = null;
    try {
        connection = BaseDeDonne.connect();
        connection.setAutoCommit(false);
        ps = connection.prepareStatement(ajoutProduits);
        for (Produits p : produits) {
            ps.setInt(1, p.getQuantite());
            ps.setString(2, p.getCategorie());
            ps.setString(3, p.getTaille());
            ps.setString(4, p.getDesignation());
            ps.setDouble(5, p.getPrixEnGros());
            ps.setDouble(6, p.getPrixUnitaire());
            ps.setDouble(7, p.getPrixVente());
            ps.setDouble(8, p.getMarge());
            ps.addBatch();
        }
        ps.executeBatch();
        connection.commit();

    } catch (SQLException b) {
        b.printStackTrace();
    } 
}

You can see the SQL query to insert objects to database:

public static final String AJOUTER_PRODUITS = "INSERT INTO " + TABLE_PRODUIT + "("
            + COLUMN_PRODUIT_QUANTITE + ","
            + COLUMN_PRODUIT_CATEGORIE+ ","
            + COLUMN_PRODUIT_TAILLE + ","
            + COLUMN_PRODUIT_DESIGANTION + ","
            + COLUMN_PRODUIT_PRIX_ACHAT + ","
            + COLUMN_PRODUIT_PRIX_REVIENT + ","
            + COLUMN_PRODUIT_PRIX_VENTE + ","
            + COLUMN_PRODUIT_PRIX_MARGE
              +")"
            + " VALUES " +
            "(" + "?" + "," + "?" + "," + "?" + "," + "?" + "," + "?" + "," + "?" + "," + "?" + "," + "?" + ")";

I tried to use a loop to insert List<Product> to the database but nothing happen to the database or I get Slite Lock database file.

0

There are 0 best solutions below