我有一些要導入的記錄。他們第一次插入時沒關係。如果我嘗試再次導入相同的數據,我收到一個org.postgresql.util.PSQLException:錯誤:重複鍵值違反了唯一性約束。如果數據相同/或更改,如何更新數據庫中的記錄,如果是使用JDBC的新數據,如何插入?使用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();
}
現在問題得到修復。我在下面提供了一個答案。
什麼是XEntity? – 2010-12-07 07:51:56
您似乎處於實施的相當早期階段。你有沒有考慮使用JPA或Hibernate來處理持久性?是否有任何需要低級別JDBC訪問的特定要求? – 2010-12-07 07:56:13
你應該想出爲什麼「this.updateEntity((XEntity)entity)== 0」。 – pinichi 2010-12-07 08:00:48