File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Update MySQL table by updating JTable's cell Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Update MySQL table by updating JTable Watch "Update MySQL table by updating JTable New topic
Author

Update MySQL table by updating JTable's cell

Shawn lee
Greenhorn

Joined: Jul 20, 2012
Posts: 3
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
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
. . . and welcome to the Ranch
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

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

Joined: Jul 20, 2012
Posts: 3
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

Joined: Jul 20, 2012
Posts: 3
Campbell Ritchie wrote: . . . and welcome to the Ranch


Yeah, it's quite nice place to learn things
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Update MySQL table by updating JTable's cell