【发布时间】:2017-12-11 07:44:14
【问题描述】:
我有一个 ERROR_MSG 表,它存储带有一些 id 的错误消息。如果表中不存在 id 并且它存在更新错误消息,我想插入错误消息。使用下面的 java JDBC 代码插入。
ID ERROR_MSG
1 ERR1
2 ERR2
3 ERR3
这是我的代码:
insertQry = "SQL";
Connection con = null;
PreparedStatement stmt = null;
try {
con = getDataSource().getConnection();
stmt = con.prepareStatement(insertQry);
for(ListingAckNackData errorList: listOfListingERROR) {
stmt.setLong(1, eqGlobalData.getSrcMsgId());
stmt.setString(2, errorList.getGliId());
if (null != errorList.getListingRevisionNo()) {
stmt.setInt(3, errorList.getListingRevisionNo());
} else {
stmt.setNull(3, Types.NULL);
}
if (null != errorList.getErrorMessage()) {
stmt.setString(4, errorList.getErrorMessage());
} else {
stmt.setNull(4, Types.NULL);
}
stmt.addBatch();
}
stmt.executeBatch();
}
【问题讨论】:
-
Oracle: how to UPSERT (update or insert into a table?) 的可能重复项,因为我认为这在 Oracle 的简单
INSERT OR UPDATE查询中是不可能的。 -
请注意,answer 在 Java 中提供了一个更简单的解决方案,但会花费一些处理...
-
@AxelH 如何在 JAVA 中使用 if ?
-
“如果”是什么意思?
-
更新表名 SET val1 = in_val1, val2 = in_val2 WHERE val3 = in_val3; IF ( sql%rowcount = 0 ) THEN INSERT INTO tablename VALUES (in_val1, in_val2, in_val3);万一;如何在上面的 java 代码的字符串 insertQry 中写这个。