2016-01-04 25 views
1

我正在使用jdbcTemplate的準備語句將一些數據寫入表中。目前,我正面臨以下例外情況,我不確定我做錯了什麼。使用PreparedStatement插入數據的JdbcTemplate更新的異常

private void insertdata(String time, String data, Datasource datasource){ 
    String sql = "insert into table1 (columns1, columns2) values (:time, :data)"; 
    jdbcTemplate jdbctemplate = new jdbcTemplate(datasource); 
    Object[] params = {time, data}; 
    int[] types = {Types.VARCHAR, Types.VARCHAR}; 
    try{ 
     jdbctemplate.update(sql, params, types); 
    }catch(Exception e){ 
     //some error handling 
    } 
    } 

我在日誌中看到以下異常。

PreparedStatementCallback; SQL [insert into table1 (columns1, columns2) values (:time, :data)]; Parameter index out of range (1 > number of parameters, which is 0).; nested exception is java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0). 

我不知道我在哪裏犯了一個錯誤。

+0

你爲什麼不使用'SqlMapParamSource'錯誤(?,?)? – TheLostMind

回答

0

你還在使用JDBC引擎蓋下 - 所以你重新編碼聲明

String sql = "insert into table1 (columns1, columns2) values (?,?)"; 

而且你會沒事的

使用NamedParameterJdbcTemplate作爲模板。

-1

的JdbcTemplate不支持命名的參數,如(:名稱,:ID)使用佔位符代替或使用是NamedParameterJdbcTemplate或SimpleJdbcDaoSupport修復