代码之家  ›  专栏  ›  技术社区  ›  PacificNW_Lover

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

  •  0
  • PacificNW_Lover  · 技术社区  · 2 年前

    使用 Java 1.8 Spring 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代码和我创建的MySQL数据库表中,已经计算了行数,它们是4。

    1 回复  |  直到 2 年前
        1
  •  2
  •   Elliott Frisch    2 年前

    您的查询具有的绑定参数 id order_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(?,?,?,?)";