• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Update MySQL table by updating JTable's cell

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a JTable which is used to show and modify table from a MySQL database. I have no problem to retrieve/show data in JTable or insert new data into a blank table. However, I have problem to update any particular cell in the JTable and reflect the changes in the MySQL database. If there are hundreds of rows and I update couple cells within around 10 rows, and I create a save button to apply all the changes to MySQL's table. what's the proper way to do that? It will include so many UPDATE statement for every row I changed, how do I detect any change I made in cells efficiently and save all the change back to the DB at once.

Thanks in advance.

Sean
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The short answer is: your TableModel should be implemented so that its setValueAt() method updates the database.

Oh wait a minute... you said you wanted a "Save" button, i.e. the database doesn't get updated until you click that. Okay. Then when you click that button, just go through all the rows of the table and update the database with whatever is in the TableModel at that point of time.

If you find that's too slow, because you're updating 8,000 rows when only 3 have been changed, then you could make setValueAt() set a flag to indicate that a row has been changed.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and welcome to the Ranch
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
However if it were me, I wouldn't have the "Save" button. I would just update the database as soon as the user changed one of the JTable cells. That makes the issue about having to update all 8,000 rows disappear.

You might want to implement an undo/redo capability, but on the other hand you don't have that in the version with the "Save" button so you might not need it in an "instant-update" version.
 
Shawn lee
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:However if it were me, I wouldn't have the "Save" button. I would just update the database as soon as the user changed one of the JTable cells. That makes the issue about having to update all 8,000 rows disappear.

You might want to implement an undo/redo capability, but on the other hand you don't have that in the version with the "Save" button so you might not need it in an "instant-update" version.




Thank you so much Paul, I am going to try your suggestion and will let you know...

 
Shawn lee
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote: . . . and welcome to the Ranch



Yeah, it's quite nice place to learn things
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic