get the data from TableView and insert into database

2k Views Asked by At

In javafx I have created a some buttons and a TableView with some columns. when I click the button, it adds the product into TableView columns. so far, no problem with that.

I have created a Product class and using this I create a product object with pID, pName, pQty, pPrice. example:

new Product(1, "Pen", 2, 3.50);
new Product(2, "notebook", 1, 5.00);

so whenever I click a product button it adds this product in next row.

Here is the problem I face:

I want to add all these rows in TableView to database I have created.

Is there a method to get all these objects one by one from TableView and insert it to database?

Or is there a method to populate database from ObservableList?

1

There are 1 best solutions below

0
On

Here is my solution. Actually I was so stupid as I was keep trying to insert all these data into another database and table with the same names. So, basically no problem of inserting. sorry for the inconvenience. and Thanks for everyone who is interested. Also, I am putting here the code I used for insertion. In case it could be useful for someone.

String query = "INSERT INTO Product (pID, pName, pQty, pPrice) VALUES (?,?,?,?)"; 
            pst = conn.prepareStatement(query);

            for(int i=0; i<data.size(); i++){

                 pst.setString(1, data.get(i).getRID().toString());
                 pst.setString(2, data.get(i).getRName());
                 pst.setString(3, data.get(i).getRQty().toString());
                 pst.setString(4, data.get(i).getRPrice().toString());


                 pst.execute();

            }

I have put this inside for loop, this way I was able to iterate each object member.