Refreshing JavaFX scene from updated SQL database? - java

I am currently writing a program which retrieves data from a SQL database and uses the data to populate a pie-chart on a JavaFX scene. So far, this has been a success.
What I am wondering is if it is possible to repopulate and refresh the pie chart as new data is entered into the database. So far the only solution I have found is to close the program and reopen it, which is not really much of a solution.
I can provide my code if needed.
Grateful for any tips you can give, thanks!

What I am wondering is if it is possible to repopulate and refresh the pie chart as new data is entered into the database.
The beauty of graphs/Charts in FX is that they are all dynamic, and change.
if you add new data to your dataCollection, or set your entire database to PieChart.setData(data);
You would have to set up a listener to listen in on your DB and changes, or listen to the changes on whatever is changing your DB. Is it external or internal to your Application?
If it's Internal than you can set up the listener with a flag and do it then, if it's External then you will either
have to poll the database x seconds/minutes
somehow set up SQL code to update something in your application and
set up a listener there (such as How to implement a db listener in Java)

Related

Update JFrame whenever MySQL table is updated

I am creating a Form for restaurant management which has a JTable showing order details. The form is connected to mysql database 'Restaurant' in which 'orders' table is present.
Now when new data is added to table 'Orders' in 'Restaurant' database I want the form to update automatically.
How can I achieve this?
Easiest solution is to create a java.util.Timer or javax.swing.Timer and assign an ActionEvent to it to check for changes in set intervals.
One of the possible solutions is using Listener Pattern.
When customer changes something in database, CustomerJFrame can notify it's listeners, that data has changed. OrdersJFrame can listen to CustomerJFrame and fetch needed data from db on notify

Use SQL without implementing a new SQL table in the program

There is a lot of tutorials out there showing me how to create a new table and ammend the data. But will creating a new table every time overwrite my existing table?
Once the table has been created and the rows of data have been added, I wouldn't need my program to create a new table again. Just read and ammend from it.
I don't want the user to create a table, so I just want the table to already be there before I even run my program.
I figured I could use run something like this to create my database tables once:
https://www.tutorialspoint.com/jdbc/jdbc-create-database.htm
Run it! And then once it is created, use this next guide to connect to where I stored it in the actual program.
https://www.tutorialspoint.com/jdbc/jdbc-select-database.htm
Be nice to find a tutorial on how to just manually make the database parameters outside of code.
The project is for university, they want me to create a client similar to MySpace. Have clients connect to a server, share music and message friends. I have done the UI and multi threaded server connections and understand how to read and write data to the UI or File. Just figured an SQL database would be the best way to store all this user data.
You just need to check the table Exist or not.
If the table exist then use it else create it and then you can use it.

How should I refresh gui that is displaying data from a mySQL database?

I am using JDBC to connect to a mySQL database in my program. During runtime, the user can modify the contents of the database by adding new ones (more functionality is coming but right now that's all I have). However, when a new entry into the database is created I want the gui that is displaying the content to update themselves with the modified data.
I know how to update the gui elements, but that doesn't seem to apply to the ResultSet created from my database.
I am quite new to mySQL and JDBC so any help would be very appreciated !
First questions:
- If it is a web application, you can use Ajax and ajax callback to manage return of your application (success or failure)
- If it is a standard application, you can manage in that way (which work from my side):
As soon as user added data, you needs to wait the result of your database. If (insert/update/delete) does not return any exception, you have to modify your GUI (add/update/remove) your display. If you have any issue you should not modify your gui.
Do not perform any select all, as you can crash your application in case of huge db.

Does SWT Table emit column created / removed events?

Skimmed through the Eclipse API doc and found no mentioning of this event. Could I have missed something? I'm building something that lists all table columns in a given table, and it needs to be automatically updated if the set of columns in the Table changes, so I need to listen to the Table structure changes and update my list accordingly.
What you are looking for doesn't exist. You could add dispose listeners to the TableColumns, and that could tell you when one is removed, but there are no events for telling you when widgets are created. You might be able to "fake it" by tracking control events on the table columns with TableColumn#addControlListener, but this is iffy. If you have control of the tables you could do something more advanced by wrapping them in a dynamic proxy that could perform that function for you.

Connection pool query results jumping between live and old data?

I'm developing a Java SE application with a MySQL database. It uses a connection pool of size 10 as there are many screens. Each screen has a Thread that will update some JTables about every 10 seconds. My connection pooling code is taken from http://java.sun.com/developer/onlineTraining/Programming/JDCBook/conpool.html with a few methods added for my own convenience.
My problem is that when I edit some of the data in the application, and save it back to the database, the JTables will now randomly display either the new updated data, or the old original data, each time the Thread runs to update the screen. It flickers back and forth between new and old, each time the thread loops around.
I also have some objects that I load by clicking on a row in the JTable, and displaying their details in textBoxes. If I click on a row with data that is having the problem above, the object which is loaded, also shows the same "old" values. (This is despite the object having got a new, different connection from the database pool to load its values)
When the JTable refreshes again, and shows the correct "updated" data - and I load the object, the object also displays the correct data.
Is this a problem with the database connection pooling library i'm using, is there a better alternative? I've tried running all my refreshing code with SQL_NO_CACHE but this has no effect. I'm new here so let me know if there's anything i'm missing from the details, thanks!
Try to change the isolation level on your connections:
connection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
Also, make sure you handle your transactions correctly, otherwise you might have random problems with phantom reads!
Edit: In order to have a correct isolation between your connections, you need to disable auto-commit and surround write operations inside a transaction. If you do not do that a read from another connection might occur in the middle of a write and it might return inconsistent data.

Categories