插入负数 - ORA-01722

时间:2017-06-28 19:06:08

标签: java string oracle-sqldeveloper jdbctemplate

我使用jdbcTemplate将一些数据插入到DataBase Oracle中。不幸的是我收到了这个错误:

java.sql.SQLSyntaxErrorException: ORA-01722: invalid Number

首先,我将字符串转换为double:

Number number = nmFormat.parse(record[8]);
double doubleVLBalance = number.doubleValue();

之后:

 String sqlSettlementBalance = "INSERT INTO STARR4.TBSETR_SETTLEMENT_BALANCE"+
                "(DT_MOVEMENT,NU_CUSTOMER_REF, NU_MOD_CUSTOMER_REF,"+
                "NU_HIERARCHICAL_NODE, CD_ACQUIRER, CD_PRODUCT,"+
                "CD_BALANCE_TYPE, NU_CUSTOMER, VL_BALANCE, NU_CONTROL)"+
                "VALUES ('" ...,'"+doubleVLBalance+"','"+sb.getNuControl()+"')";

jdbcTemplate.update(sqlSettlementBalance);

字符串记录[8]的值是" -0.34"。有什么建议可以解决这个问题吗?

2 个答案:

答案 0 :(得分:3)

如果用引号括起一个数字,它就会变成一个字符串。删除行情。

 String sqlSettlementBalance = "INSERT INTO STARR4.TBSETR_SETTLEMENT_BALANCE"+
            "(DT_MOVEMENT,NU_CUSTOMER_REF, NU_MOD_CUSTOMER_REF,"+
            "NU_HIERARCHICAL_NODE, CD_ACQUIRER, CD_PRODUCT,"+
            "CD_BALANCE_TYPE, NU_CUSTOMER, VL_BALANCE, NU_CONTROL)"+
            "VALUES ('" ...,"+doubleVLBalance+",'"+sb.getNuControl()+"')";

答案 1 :(得分:3)

如前所述,您应该删除引号,但我建议而不是

jdbcTemplate.update(sqlSettlementBalance);

使用

jdbcTemplate.update(sqlSettlementBalance, Object... args);

例如:

 String sqlSettlementBalance = "INSERT INTO STARR4.TBSETR_SETTLEMENT_BALANCE"+
            "(DT_MOVEMENT,NU_CUSTOMER_REF, NU_MOD_CUSTOMER_REF,"+
            "NU_HIERARCHICAL_NODE, CD_ACQUIRER, CD_PRODUCT,"+
            "CD_BALANCE_TYPE, NU_CUSTOMER, VL_BALANCE, NU_CONTROL)"+
            "VALUES ('"...,?,'"+sb.getNuControl()+"')";

jdbcTemplate.update(sqlSettlementBalance, doubleVLBalance);

使用PreparedStatement参见示例this