使用 JDBC 插入或更新表
我有一些记录要导入。第一次插入时没问题。如果我尝试再次导入相同的数据,我会收到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以尝试使用 postgres SQL 'MERGE' 或 'REPLACE'
You can try using postgres SQL 'MERGE' or 'REPLACE'
您可以通过 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.
如果您想使用相同的方法插入和更新数据,则需要先检查数据是否存在。用于插入新对象的 SQL 命令是
INSERT
,而用于更新元素的 SQL 命令是UPDATE
。因此,您可以执行SELECT
来检查您的数据是否已在此处,然后根据数据执行INSERT
或UPDATE
结果。然而,这是一个解决方法。您确实需要澄清您的实现,并无论是添加还是更新数据都采用不同的方法。在业务方面,这显然是两个截然不同的功能,因此在我看来,两者的一种方法似乎是一个设计问题。
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 isUPDATE
. So, what you could do is do aSELECT
to check if your data is already here, and then do anINSERT
orUPDATE
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.
该测试逻辑有效。
问题出在更新记录的方法上。 update准备语句中的WHERE子句使用了不同的数据(包含空格的数据),因此updateEntity将始终返回0。这就是为什么只进行插入而不进行更新的原因。非常感谢您的帮助。
This test logic works.
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.