Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Marek Krokosinski
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21572
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic