Forums Register Login

How to retrieve (refresh) data from db without restarting app?

+Pie Number of slices to send: Send
Hello.

I have an application made in Swing.
It displays data from database, but if something in db will be changed, for example I will insert new record to the table, I can't see it in my application. I will see the new record only after the application will be restarted. Is there any method that allow to refresh the data from db without restart? I have tried with JPanel validate()/repaint() but without success.

Best regards.
+Pie Number of slices to send: Send
The short answer is that you would have to write code which goes out to the database and gets the data you want refreshed.

Of course there is nothing that will tell your application that some other application has updated the database, so you cannot expect this code to be run automatically. Either you would have to have a process that ran it periodically, or a button which allowed the user to run it on demand.

What you do, then, depends on your particular environment and your requirements.
+Pie Number of slices to send: Send
I don't know threads, so I will pass on that solution for the moment.

I have menu button, which could refresh my data. Then it will be something like this:

JButton pressed -> getConnection with db -> refresh data -> display list of articles -> disconnect with db. I don't know if this is good or not, but I think that I'm not able to do something more ambicious for the moment.

Anyway this is what I have tried to do earlier with validate/repaint methods, but it didn't worked. Now my question is, is there any method that refresh the data? Or I have to delete my current data from JPanel and add it once again (updated ofcourse) ?
+Pie Number of slices to send: Send
It seems yours is a Swing application. In that case, it would probably be better to open a connection at the start of the application and close it at the end; connecting to the database is generally an expensive operation. Some JDBC drivers might provide transparent connection caching behind the scenes, though.

When refreshing the data, you should not need to shuffle components on the JPanel. If you're using JTable with DefaultTableModel, you'll simply modify the model and the table will reflect that automatically. If you're using your own implementation of the model, you should use proper fireTableXxx methods to advertise changes to the table. Your own implementation of the model can be more efficient, but is generally a bit harder to implement properly; if you're new to Swing, use DefaultTableModel.

You definitely should not need to call repaint, unless you're implementing your own Swing components.
+Pie Number of slices to send: Send
I'm not using JTable, because I need to get only data from one column (in the place where I want to have fresh data). For now I have added additional JButton, which removes all components from JPanel and gets new data from db by connecting to it. I know it's a bad solution, but I couldn't handle it in other way.
+Pie Number of slices to send: Send
Which component(s) do you use to display the data, then? (JTable can be used to display just one column too.)

Refactoring the app to use one connection instead of connecting and disconnecting all the way can be a big effort, that's true.
Is that a spider in your hair? Here, threaten it with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1185 times.
Similar Threads
B&S Find in .db or in memory?
booking Procedure
About create new record number for URLyBird
design question: 2 application A & B; needs to be in sync when new data is created on B. howto?
Starting OCMJD
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 12:00:04.