java batchUpdate()抛出超出范围的TransientDataAccessResourceException参数索引(4 > 3)

axr492tv  于 2023-06-20  发布在  Java
关注(0)|答案(1)|浏览(139)

使用Java 1.8Spring Framework 4.0.3-RELEASE,我试图在从外部提要获取值后将一行插入MySQL数据库。
试试这个:

private static JdbcTemplate jdbcTemplateObject = null;
private static final String INSERT_QUERY = "insert into order_table(id,order_id,created_time,updated_time)VALUES(?,?,now(),now())";

parseFeedAndStoreIntoDB() {
    List<Object[]> insertData = new ArrayList<>();
    SqlRowSet sqlRowSet = null;

    // id, order_id, created_time, updated_time have values 
    insertData.add(new Object[] {id, order_id, created_time, updated_time});
    if (insertData.size() > 0) {
        // It breaks here
        jdbcTemplateObject.batchUpdate(INSERT_QUERY, insertData);
    }
}

当我运行这个方法时,我得到以下异常:

Exception in parseFeedAndStoreIntoDB() method
org.springframework.dao.TransientDataAccessResourceException: PreparedStatementCallback; SQL 
[insert into order_table(id,order_id,created_time,updated_time)VALUES(?,?,now(), now());]; Parameter index out of range (4 > 3).; nested exception is java.sql.SQLException: 
Parameter index out of range (4 > 3).

我已经数了行,它们在我的Java代码中是4,在我创建的MySQL数据库表中也是4。

tvmytwxo

tvmytwxo1#

您的查询具有idorder_id的绑定参数,其他两个字段在查询中设置为now()。变更

insertData.add(new Object[] {id, order_id, created_time, updated_time});

insertData.add(new Object[] {id, order_id});

更改

private static final String INSERT_QUERY = "insert into "
        + "order_table(id,order_id,created_time,updated_time) "
        + "VALUES(?,?,now(),now())";

private static final String INSERT_QUERY = "insert into "
        + "order_table(id,order_id,created_time,updated_time) "
        + "VALUES(?,?,?,?)";

相关问题