Forums Register Login

Update MySQL table by updating JTable's cell

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
. . . and welcome to the Ranch
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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...

+Pie Number of slices to send: Send
 

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



Yeah, it's quite nice place to learn things
Straws are for suckers. Now suck on this tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 2660 times.
Similar Threads
JTable
JTABLE
Populating data into JTable
Save function for JTable in connection with the database
JTable Problem...
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 11:57:31.