首頁  >  問答  >  主體

jdbcTemplateObject.batchUpdate() 引發了一個 TransientDataAccessResourceException 例外,參數索引超出了範圍(4 > 3)

使用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有值
    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行。

P粉256487077P粉256487077403 天前422

全部回覆(1)我來回復

  • P粉647504283

    P粉6475042832023-09-13 09:02:50

    您的查詢在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(?,?,?,?)";

    回覆
    0
  • 取消回覆