Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Updating Database using JTables

 
Joshua Ebarvia
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34095
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Brian Cole
Author
Ranch Hand
Posts: 902
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Clapham
Sheriff
Pie
Posts: 20758
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or turn autoCommit off and commit upon saving.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic