Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question

1 Answer

In JavaFX you usually get to be the person to create the example :)

ObservableList supports listeners, these receive events which tell you what has been added or updated by default. There is a good example in the javadocs here.

To get update events you need to provide an 'extractor' to the method creating the list here. This should take an instance of the object in the list and provide an array of the properties you want to listen to.

share|improve this answer
Thank you for the info. However, I don't understand how this helps me tie my ObservableList to my SQL Insert statement. The listener property is very helpful and the reason I used ObservableList to display my data in TableView. But how do I now use it to write an insert statement? – John May 18 at 11:51
If you receive an event with added items then you can insert them, update on updated items etc. – Andy Till May 18 at 12:19
I'm sure this makes perfect sense to someone who has done this, but this answer does not help me, who has never seen this in action. Can you point me to some code where this is actually functional? – John May 18 at 12:32
Do you just need to iterate a list and insert each item? Google "java foreach" and "jdbc insert tutorial", this is well trodden ground with good code examples online. Anywhere you see List or ArrayList in java you can use an ObservableList in the exact same way. – Andy Till May 18 at 12:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.