This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Updating Database using JTables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Updating Database using JTables" Watch "Updating Database using JTables" New topic
Author

Updating Database using JTables

Joshua Ebarvia
Ranch Hand

Joined: Sep 25, 2007
Posts: 70
Hi guys!

I have a JFrame with a save button and cancel button, a jTable used to display data from my mysql database. I use a jDialog to insert record to the database and my jTable will display the records. However, what I want is this, If I click the cancel button in my JFrame, the recently added records in my database which is displayed in my JTable be deleted, since the cancel button is clicked which voids every addition or editing of the data in the database.

How can I cancel the saved records from the database if the cancel button is clicked. It is like restoring the jtable and its display to its original state when cancel button is pressed.

Thanks..

I'm having a hard time to explain my problem..
Please help.


Lovin' java
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 32628
    
215

Joshua,
Welcome to JavaRanch!

I'm not sure if your question is about databases or JTables. I'm going to move it to our GUI forum where they know about JTables. (since JDBC is inundated with the promo this week) If it winds up being more about JDBC, someone can move it back.


[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 877
    
    1
Originally posted by Joshua Ebarvia:
How can I cancel the saved records from the database if the cancel button is clicked. It is like restoring the jtable and its display to its original state when cancel button is pressed.


Either you clear the table and reload from the database (or I guess you could use a cache), or else you keep track of which rows the user added so they can be removed later.


bitguru blog
Paul Clapham
Sheriff

Joined: Oct 14, 2005
Posts: 19747
    
  10

Basically, you keep track of what you added (by storing the information in some kind of a list) and if the user pressed Cancel, you go through that list and delete those records.

Just remember that your database table has to have a primary key or keys for this to work, otherwise you have no way of getting a record to delete it.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 20131
    
  30

Or turn autoCommit off and commit upon saving.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Updating Database using JTables
 
jQuery in Action, 3rd edition