我有一些记录要导入。第一次插入时没问题。如果我尝试再次导入相同的数据,我会收到 org.postgresql.util.PSQLException:错误:重复的键值违反唯一约束。如果数据相同/或更改,如何更新数据库中的记录,如果是新数据,如何使用 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();
}
问题现已解决。我在下面提供了答案。
最佳答案
您可以尝试使用 postgres SQL 'MERGE' 或 'REPLACE'
关于java - 使用 JDBC 插入或更新表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4374383/