我有一些记录要导入。第一次插入时没关系。如果我尝试再次导入相同的数据,我会收到 org.postgresql.util.PSQLException: ERROR: duplicate key value 违反唯一约束。如果数据相同/或已更改,我如何更新数据库中的记录,如果它是使用 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();
}
现在问题已解决。我在下面提供了答案。