queryRunner插入添加一个参数

时间:2018-08-23 20:33:22

标签: java oracle jdbc apache-commons-dbutils

我正在尝试插入并获取自动生成的ID。这是一个Oracle数据库。为此,我使用的是org.apache.commons.dbutils.QueryRunner插入,该插入返回第一列。

问题是我不知道我是出于什么原因在查询中添加了一个参数,因此它无法正常工作。

我有此代码:

import javax.annotation.Nonnull;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import javax.sql.DataSource;     
import java.sql.SQLException;
import java.util.Optional;

@Singleton
public class ConceptToBonusRepository extends GenericSQLRepository{

    private static final String QUERY_SAVE_CONCEPT_TO_BONUS = "INSERT INTO concept_to_bonus (ID, CONCEPT_ID, CONCEPT_TYPE)" +
        " VALUES (SEQ_CONCEPT_TO_BONUS_ID.NEXTVAL,?,?)";

    @Inject
    public ConceptToBonusRepository(@Named("OracleDataSource") @Nonnull DataSource dataSource) {
        super(dataSource);
    }

    public Optional<Long> saveConceptToBonus(ConceptToBonus conceptToBonus) 

        try {
            return Optional.ofNullable(
               runInsert(
                    QUERY_SAVE_CONCEPT_TO_BONUS, conceptToBonus.getConceptId(), conceptToBonus.getConceptType()
               )
           );
        } catch (SQLException e) {
        throw new RuntimeException(e);
        }

    }
}

和我的GenericlSQLRepository

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import javax.annotation.Nonnull;
import javax.sql.DataSource;
import java.sql.SQLException;

public abstract class GenericSQLRepository {

    private QueryRunner queryRunner;
    private ScalarHandler<Long> autogeneratedIdHandler = new ScalarHandler<>();

    protected GenericSQLRepository(@Nonnull final DataSource dataSource) {
        this.queryRunner = new QueryRunner(dataSource);
    }

    protected Long runInsert(@Nonnull final String sql,
                            Object... args) throws SQLException {
        return queryRunner.insert(sql, this.autogeneratedIdHandler, args);
    }
}

当我尝试运行此错误时,我会收到此错误

  

“ java.sql.SQLException:错误的参数数量:预期为3,给出了2查询:INSERT INTO concept_to_bonus(ID,CONCEPT_ID,CONCEPT_TYPE)VALUES(SEQ_CONCEPT_TO_BONUS_ID.NEXTVAL,?,?)参数:[1731472066,ORDER] “

我真的不明白为什么要在参数计数中添加一个参数。当我通过一个简单的执行程序运行此插入程序时,它工作得很好

0 个答案:

没有答案