参数实例recId对于请求的转换无效。 ERRORCODE = -4461

时间:2016-07-25 18:49:48

标签: spring jdbc db2 jdbctemplate named-parameters

我使用spring JdbcNamedTemplate在数据库中插入数据。对于其他操作(例如删除),它工作正常,但插入时我从DB2得到-4461错误。

我已经去了这些页面: http://www-01.ibm.com/support/docview.wss?uid=swg21622381

Invalid data conversion: Parameter instance 50.0/100 is invalid for the requested conversion. ERRORCODE=-4461, SQLSTATE=42815

但没有帮助。下面是我的代码片段。

@Override
public void insert(FLIGHT fw) {
    String sql = "INSERT INTO FLIGHT (FLT,LEG,ORG,DST,BOOKED) VALUES  (:flt,:leg,:org,:dst,:booked)";


MapSqlParameterSource namedParameters = new MapSqlParameterSource();
namedParameters.addValue("flt", fw.getFlightNumber());
namedParameters.addValue("leg", fw.getFlightLeg());
namedParameters.addValue("org", fw.getOrg());
namedParameters.addValue("dst", fw.getDst());
namedParameters.addValue("booked", fw.getBooked());
getNamedParameterJdbcTemplate().update(sql, namedParameters);

}

此处还有我的db2

的Flight Table架构
CREATE TABLE FLIGHT
(
    FLT VARCHAR(4) NOT NULL,
    LEG VARCHAR(1) NOT NULL,
    ORG VARCHAR(3),
    DST VARCHAR(3),
    BOOKED DECIMAL(3),
    REC_ID INT NOT NULL GENERATED ALWAYS AS IDENTITY(START WITH 1 INCREMENT BY 1),
    CONSTRAINT PK_FLIGHT PRIMARY KEY (FLT, LEG)
);
CREATE UNIQUE INDEX UQ268_FLIGHT ON FLIGHT(REC_ID);

这就是我得到的错误

Caused by: com.ibm.db2.jcc.am.SqlSyntaxErrorException: [jcc][1091][10404][4.13.80] Invalid data conversion: Parameter instance REC_ID is invalid for the requested conversion. ERRORCODE=-4461, SQLSTATE=42815
    at com.ibm.db2.jcc.am.id.a(id.java:677)
    at com.ibm.db2.jcc.am.id.a(id.java:60)
    at com.ibm.db2.jcc.am.id.a(id.java:103)
    at com.ibm.db2.jcc.am.kc.a(kc.java:774)
    at com.ibm.db2.jcc.am.kc.a(kc.java:689)
    at com.ibm.db2.jcc.am.go.a(go.java:1583)
    at com.ibm.db2.jcc.am.go.setString(go.java:1538)
    at org.jboss.jca.adapters.jdbc.WrappedPreparedStatement.setString(WrappedPreparedStatement.java:637)
    at org.springframework.jdbc.core.StatementCreatorUtils.setValue(StatementCreatorUtils.java:421) [spring-jdbc-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValueInternal(StatementCreatorUtils.java:235) [spring-jdbc-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValue(StatementCreatorUtils.java:150) [spring-jdbc-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.jdbc.core.PreparedStatementCreatorFactory$PreparedStatementCreatorImpl.setValues(PreparedStatementCreatorFactory.java:292) [spring-jdbc-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.jdbc.core.PreparedStatementCreatorFactory$PreparedStatementCreatorImpl.createPreparedStatement(PreparedStatementCreatorFactory.java:244) [spring-jdbc-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:623) [spring-jdbc-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    ... 200 more

1 个答案:

答案 0 :(得分:0)

您可以在代码中尝试此操作

namedParameters.addValue("flt", fw.getFlightNumber(),Types.VARCHAR);
namedParameters.addValue("leg", fw.getFlightLeg(),Types.VARCHAR);
namedParameters.addValue("org", fw.getOrg(),Types.VARCHAR);
namedParameters.addValue("dst", fw.getDst(),Types.VARCHAR);
namedParameters.addValue("booked", fw.getBooked(),Types.DOUBLE)

这次运行正确,

PreparedStatement ps = connection.prepareStatement(
            "INSERT INTO FLIGHT (FLT,LEG,ORG,DST,BOOKED) VALUES  (?,?,?,?,?)");
            ps.setString(1,"b");
            ps.setString(2 ,"b");
            ps.setString(3 ,"b");
            ps.setString(4 ,"b");
            ps.setDouble(5 ,1);
            ps.executeUpdate();
            ps.close();


user@host:/db2inst1:>db2 "select * from FLIGHT"

FLT  LEG ORG DST BOOKED REC_ID     
---- --- --- --- ------ -----------
a    a   a   a       1.           1
b    b   b   b       1.           2

  2 record(s) selected.