Java and jdbc issue: How to update jtable row containing primary key?

I've search thoroughly for the solution to this problem but with no success. All related solutions apply to inserting new records. I have found non for updating existing records.

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

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

 private void updateSelected() {
                try {
        // Get the current row
        int row = jTable1.getSelectedRow();

        // Gather data from the UI and update the database fields
        for (int i = 1;
          i <= resultSet.getMetaData().getColumnCount(); i++) {
          resultSet.updateObject(i, tableModel.getValueAt(row, i - 1));

        // Invoke the update method in the result set

      catch (Exception ex) {

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 occasions the update works without showing this error. How can I resolve this issue. Your advice would be much appreciated.


Need Your Help

Magento Low Stock Report

magento magento-1.7

I am using Magento and have noticed that my Low Stock report is always blank. Is there anything specific I need to do to have this report display my inventory stock levels? I have Magento s...

IBM Worklight server not starting on Windows 8.1 Pro 64 bits

java eclipse worklight

I am trying to use Worklight on Windows 8.1 Pro 64 bits.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.