Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3492404
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:45:16+00:00 2026-05-18T11:45:16+00:00

I have some records to import. It’s ok the first time when they are

  • 0

I have some records to import. It’s ok the first time when they are being inserted. If I try to import the same data again I receive a org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint. How can I update the records in the database if the data is the same/or changed and insert if it’s new data using JDBC?

public void store(Object entity) throws Exception {
    try {
        if (this.updateEntity((XEntity) entity) == 0) {
            this.insertEntity((XEntity) entity);
        }

        ...

    } catch (SQLException sqlEx) {
        ...
    }
}

private int updateEntity(XEntity entity) throws SQLException {
    PreparedStatement prepStmt = this.getUpdatePreparedStmt();
    ...
    return prepStmt.executeUpdate();
}

private void insertEntity(XEntity entity) throws SQLException {
    ...
    this.getInsertPreparedStmt().executeUpdate();
}

The problem is fixed now. I’ve provided an answer below.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T11:45:16+00:00Added an answer on May 18, 2026 at 11:45 am

    This test logic works.

    if (this.updateEntity((XEntity) entity) == 0) {
        this.insertEntity((XEntity) entity);
    }
    

    The problem was in the method that updated the record. The WHERE clause in the update prepared statement was using different data(data containing spaces) so updateEntity would always return 0. That was the reason why only inserts were made, instead of updates. Thank you very much for your help.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some records like this: ID Personel_Code Time --- ------------- ------ 1 0011
I have some XML which contains records and sub records, like this: <data> <record
I have created a temporary table using cakephp $this->Model->query() and inserted some records using
I have some records after SQL generating: YEARS MONTHS SUMMONTH SUMQUARTER QTR ----- ------
I have some dirty resource usage records in t_resourcetable which looks like this resNo
i have SOQL query which queries for some records based on a where condition.
I have created a View which prompts the user to input some records with
Hi experts: I have a table Called tblAlarm and it has some records like
I have a table within my database that has many records, some records share
I have a dataset with some 30 records in it. I want to update

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.