在DB2上使用JDBC更新异常

时间:2013-03-21 15:33:10

标签: java jdbc db2

导致异常的代码如下:

    for (int i = 0; i < updateParams.size(); i++) {
        s = con.prepareStatement(sSQL);
        params = (String[][]) updateParams.get(i);
        setParamsPreparedStatement(s, params);
        log.debug("executeUpdates: " + sSQL + "[params:" + Arrays.deepToString(params) + "]");
        Date d = new Date();
        s.execute();
        log.info("STATEMENT EXECUTE TIME IN SECOND=" + (new Double(new Date().getTime() - d.getTime()) / 1000));
        rowcount += s.getUpdateCount();
        s.close();
    }

异常的确切行是s.execute。

stracktrace是:

com.ibm.db2.jcc.c.SqlException: The value of a host variable in the EXECUTE or OPEN statement is out of range for its corresponding use.
    at com.ibm.db2.jcc.c.fg.d(fg.java:1340)
    at com.ibm.db2.jcc.b.gb.k(gb.java:351)
    at com.ibm.db2.jcc.b.gb.a(gb.java:60)
    at com.ibm.db2.jcc.b.w.a(w.java:52)
    at com.ibm.db2.jcc.b.wb.c(wb.java:213)
    at com.ibm.db2.jcc.c.gg.ab(gg.java:1779)
    at com.ibm.db2.jcc.c.gg.d(gg.java:2324)
    at com.ibm.db2.jcc.c.gg.d(gg.java:2420)
    at com.ibm.db2.jcc.c.gg.X(gg.java:1332)
    at com.ibm.db2.jcc.c.gg.execute(gg.java:1316)

插入查询是:

insert into CMP_RULES_ACTIONS (RULE_ID, ACTION_ID, CAMPAIGN_ID, creation_date, LAST_UPDATE_DATE, expiry_date, activation_date, enabled, priority, GROUP_ID, owner) values (?, ?, ?, sysdate, sysdate, to_date(?,'YYYY/MM/DD HH24:MI'), to_date(?,'YYYY/MM/DD HH24:MI'), 1, ?, ?, ?)

参数是:

[params:[[freebuy_wv_scomm_bonus_perc_863, 12], [sendgenericbonuslist, 12], [863, 4], [2101/01/31 00:00, 12], [2013/03/21 16:14, 12], [0, 4], [null, 12], [null, 12]]]

谷歌搜索异常消息什么都没有。我是DB2的新手。有人能帮助我吗?

更新

我在预备陈述中如何设置参数的示例:

public void setParamsPreparedStatement(PreparedStatement s, String[][] params) throws Exception {
    log.debug("Params are: " + Arrays.deepToString(params));
    for (int i = 0; i < params.length; i++) {
        if (params[i][1].equals(ParamTypes.ORA_TYPE_INTEGER)) {
            if (params[i][0] != null && !params[i][0].trim().equals("")) {
                s.setInt(i + 1, Integer.parseInt(params[i][0]));
            } else {
                s.setNull(i + 1, Types.INTEGER);
            }
        }

 ...
}

我试图将一根长串放入一个太短的字段中。现在该字段更长,我的代码成功。

1 个答案:

答案 0 :(得分:1)

问题是一个字段,即RULE_ID,对于我试图插入的值来说太短(VARCHAR(30))(&#34; freebuy_wv_scomm_bonus_perc_863&#34;)。