javafx-2sql-insertobservablelist

Insert/Update SQL table from observablelist


Ok, so I'm working with an ObservableList, which is working fine, but now I need to use the observable list to insert rows into and update rows in an SQL database table. I've found little info on working between JavaFX and SQL databases ... all the examples of data tables have the data created in the java code. I had hope when I saw "update SQL database" in this post:

Update sql database from FoxPro data on Glassfish server

but it was not applicable to my situation.

So the question is, how do I start the code to read from the ObservableList so I can run my SQL Insert statement? If you could point me to an example code where an ObservableList is used and an SQL table is created/added to/updated I would greatly appreciate it.

Thanks!

UPDATE TO QUESTION:

I can't really post relevant code here because the relevant parts are what I don't have. However, I'm thinking what I need to do is something like this:

mylist.moveToFirst();
while (mylist.next()) {
    make connection // think I got it
    INSERT INTO mytable (name, address, phone) VALUES (observablename, observableaddress, observablephone // think I got this as well

Obviously I'm applying my knowledge of other areas to ObservableList, but I am doing it to demonstrate what I don't know how to do with my ObservableList (mylist).

Again, thanks for any help.


Solution

  • Tying up loose ends today, and this question has not really been answered. I reposted a newer question with more specifics once I learned more about the situation, and that question also went unanswered, but I did figure it out, and posted an answer here: Understanding my ObservableList.

    However, to be neat and tidy, let me post here some code to help me remember, as well as help anyone else who looks at this question and says, "YES, BUT WHAT IS THE SOLUTION?!?!?"

    Generically, it looks something like this:

    1. I like to open my connection and prepare my statement(s) first.
    2. Use the iterator to get the variables from the list
    3. within the iterator, add the variables to the prepared statement and execute.

    I read somewhere about batch execution of statements, but with as few updates as I'm doing with each list, that seemed too complicated, so I just do each update individually within the iterator.

    Specifically, here is some code:

    Connection con;
    con = [your connection string]; // I actually keep my connection string in its own class
            // and just call it (OpenDB.connect()). This way I can swap out the class OpenDB
            // for whatever database I'm using (MySQL, MS Access, etc.) and I don't have to
            // change a bunch of connection strings in other classes.
    PreparedStatement pst;
    String insertString = "INSERT INTO People (Name, Address, Phone) VALUES (?, ?, ?)";
    pst = con.prepareStatement(insertString);
    
    for(Person p : mylist) { // read as: for each Person [a data model defined in a class
            // named Person] which in this set of statements we shall call 'p' within the list
            // previously defined and named 'mylist' ... or "For each Person 'p' in 'mylist'"
        String name = p.name.get(); // get the name which corresponds to the Person in this object of 'mylist'
        String address = p.address.get(); // ditto, address
        Integer phone = p.phone.get(); // ditto, phone. Did as integer here to show how to add to pst below
    
        pst.setString(1, name); // replace question mark 1 with value of 'name'
        pst.setString(2, address); // ditto, 2 and 'address'
        pst.setInt(3, phone); // ditto, 3 and 'phone'
    
        pst.executeUpdate();
    

    And that's how I did it. Not sure if it's the 'proper' way to do it, but it works. Any input is welcomed, as I'm still learning.