wood burning stoves 2.0*
The moose likes JDBC and the fly likes jdbc  problem: How to update jtable  row containing primary key? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "jdbc  problem: How to update jtable  row containing primary key?" Watch "jdbc  problem: How to update jtable  row containing primary key?" New topic
Author

jdbc problem: How to update jtable row containing primary key?

Tamas Tiote
Greenhorn

Joined: Dec 17, 2011
Posts: 2
I've search thouroughly for the solution to this problem but with no success. All related solutions apply to insertint new records. I have found non for updating the database table.

I have a database with containing a primary key (with auto increment attribute). I then present the databse content in a jtable interface.
Now, I will want the database to be updated from the jtable interface.

This is the function I try to use to accomplish that:



However, I get the following error when update button is hit:

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '4' for key 'PRIMARY'

The strange thing is that on a few ocasions the update works without showing this error. How can I resolve this issue. Your advice would be much appreciated.
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 347
Could there be any possibility that you are trying to update a primary key value with one that already exists in the table?
Tamas Tiote
Greenhorn

Joined: Dec 17, 2011
Posts: 2
Gamini Sirisena wrote:Could there be any possibility that you are trying to update a primary key value with one that already exists in the table?


I think this is exactly what is happening. I simply select and show all columns of the database (including the primary key) in a jtable and then use a button to update the database when changes are made to the table.

Actually, the primary key is not changed but its value is included in the data being updated (saved to the database).

Is there a better way to do this (i.e. using jtable to modify my database table)?

Thank you.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: jdbc problem: How to update jtable row containing primary key?
 
Similar Threads
JTable
JTable and JScrollPane problem
JTable
JTable
JTable