Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Update MySQL table by updating JTable's cell

 
Shawn lee
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and welcome to the Ranch
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Campbell Ritchie wrote: . . . and welcome to the Ranch


Yeah, it's quite nice place to learn things
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic