列索引超出范围:1,列数:0

时间:2016-08-02 12:40:21

标签: java postgresql jdbc postgresql-9.4 cachedrowset

我更新了UPSERT_DELETE_JOB_COUNTERS。 现在我认为" upsert"使用新的Query =)或者你怎么看?

的问候, 彼得

`private static final String UPSERT_DELETE_JOB_COUNTERS =
        "UPDATE rating.delete_job_counters SET delete_count = delete_count + ? WHERE fee_group_id = ? AND delete_job_id = ?; "+
        "INSERT INTO  rating.delete_job_counters(delete_job_id, fee_group_id, delete_count ) "+
        "SELECT ?, ? , ? " +
        "WHERE NOT EXISTS (SELECT 1 FROM rating.delete_job_counters  WHERE fee_group_id = ? AND delete_job_id = ?); ";

@Override
public Boolean insertIntoDeleteStatistic(final CachedRowSet deletedEntries,
                                      Long deleteJobId,
                                      Transaction transaction) throws SQLException {
    boolean finish = true;
    if (deletedEntries.size() == 0) {
        return finish;
    }
    final Connection connection = transaction.getConnection();
    final Timer executeTimer = Timer.start("Insert took ");
    try {
        PreparedStatement pstmt = connection.prepareStatement(UPSERT_DELETE_JOB_COUNTERS);
        while (deletedEntries.next()) {
            pstmt.setInt(1, deletedEntries.getInt(2));
            pstmt.setInt(2, deletedEntries.getInt(1));
            pstmt.setLong(3, deleteJobId);
            pstmt.setLong(4, deleteJobId);
            pstmt.setInt(5, deletedEntries.getInt(1));
            pstmt.setInt(6, deletedEntries.getInt(2));
            pstmt.setInt(7, deletedEntries.getInt(1));
            pstmt.setInt(8, deletedEntries.getInt(2));
            pstmt.execute();
        }...

`

1 个答案:

答案 0 :(得分:-1)

您不能将参数化java.sql.PreparedStatementDO语句一起使用。

为什么不使用INSERT ... ON CONFLICT DO UPDATE(从PostgreSQL 9.5开始提供)?

对于旧版本的PostgreSQL,您可能需要像这个伪代码样本一样编写“无限循环”:

while (1) {
   INSERT ...
   if (insert went ok) break;
   UPDATE ...
   if (update changed one row) break;
}