aspose file tools
The moose likes JDBC and the fly likes How to retrieve (refresh) data from db without restarting app? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Databases » JDBC
Reply Bookmark "How to retrieve (refresh) data from db without restarting app?" Watch "How to retrieve (refresh) data from db without restarting app?" New topic
Author

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

Marek Krokosinski
Ranch Hand

Joined: Jun 10, 2011
Posts: 64
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.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16483
    
    2

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.
Marek Krokosinski
Ranch Hand

Joined: Jun 10, 2011
Posts: 64
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) ?
Martin Vajsar
Bartender

Joined: Aug 22, 2010
Posts: 2331
    
    2

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.
Marek Krokosinski
Ranch Hand

Joined: Jun 10, 2011
Posts: 64
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.
Martin Vajsar
Bartender

Joined: Aug 22, 2010
Posts: 2331
    
    2

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.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: How to retrieve (refresh) data from db without restarting app?
 
Similar Threads
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
B&S Find in .db or in memory?
booking Procedure