gpt4 book ai didi

java - 使用 JDBC 插入或更新表

转载 作者:太空宇宙 更新时间:2023-11-04 08:45:50 25 4
gpt4 key购买 nike

我有一些记录要导入。第一次插入时没问题。如果我尝试再次导入相同的数据,我会收到 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com