使用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有值
insertData.add(new Object[] {id, order_id, created_time, updated_time});
if (insertData.size() > 0) {
// 这里出错了
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行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
您的查询在
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(?,?,?,?)";