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
        resultSet.updateRow();

      }
      catch (Exception ex) {
        jlbl.setText(ex.toString());
      }
    }

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.

Answers


Need Your Help

Magento Low Stock Report

magento magento-1.7

I am using Magento 1.7.0.2 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 6.1.0.1 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.