Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Tamas Tiote
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic