使用 JDBC 插入或更新表

发布于 2024-10-06 12:58:49 字数 736 浏览 0 评论 0原文

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

问题现已解决。我在下面提供了答案。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

森林散布 2024-10-13 12:58:49

您可以尝试使用 postgres SQL 'MERGE' 或 'REPLACE'

You can try using postgres SQL 'MERGE' or 'REPLACE'

压抑⊿情绪 2024-10-13 12:58:49

您可以通过 JDBC 将 UPDATE 命令作为字符串传递。

根据 这篇 帖子,您必须编写 2 条语句。

You can pass the UPDATE command as a string through JDBC.

According to this SO post, you will have to write 2 statements.

哽咽笑 2024-10-13 12:58:49

如果您想使用相同的方法插入和更新数据,则需要先检查数据是否存在。用于插入新对象的 SQL 命令是 INSERT,而用于更新元素的 SQL 命令是 UPDATE。因此,您可以执行 SELECT 来检查您的数据是否已在此处,然后根据数据执行 INSERTUPDATE结果。

然而,这是一个解决方法。您确实需要澄清您的实现,并无论是添加还是更新数据都采用不同的方法。在业务方面,这显然是两个截然不同的功能,因此在我看来,两者的一种方法似乎是一个设计问题。

If you want to use the same method to insert and update your data, you'll need to check if the data exists first. The SQL command used to insert a new object is INSERT, whereas the one used to update an element is UPDATE. So, what you could do is do a SELECT to check if your data is already here, and then do an INSERT or UPDATE based on the results.

However, this is a workaround. You would really need to clarify your implementation, and make different methods whether you are adding or updating data. Business-side, these are clearly two very different functions, so one method for both seems to me like a design problem.

不…忘初心 2024-10-13 12:58:49

该测试逻辑有效。

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

问题出在更新记录的方法上。 update准备语句中的WHERE子句使用了不同的数据(包含空格的数据),因此updateEntity将始终返回0。这就是为什么只进行插入而不进行更新的原因。非常感谢您的帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文