1

您好我正在尝试使用 Spring JDBC 执行存储过程。这是SP类

 class IncrementExtraBalanceStoredProcedure extends StoredProcedure {

    /**
     * @param jdbcTemplate
     * @param procedureName
     */
    public IncrementExtraBalanceStoredProcedure(JdbcTemplate jdbcTemplate, String procedureName) {
        super(jdbcTemplate, procedureName);
        declareParameter(new SqlOutParameter(O_RETURN_CODE, Types.INTEGER));
        declareParameter(new SqlParameter(P_NUMEC, Types.INTEGER));
        declareParameter(new SqlParameter(P_GBYTES, Types.INTEGER));
        compile();
    }

    /**
     * @param inputBean
     * @return resultObjects
     */
    public Map<String, Object> execute(RateLimitLogBean inputBean) {
        Map<String,Object> sqlMap = new HashMap<String,Object>();
        sqlMap.put(P_NUMEC, inputBean.getNumec());
        sqlMap.put(P_GBYTES,  inputBean.getGb());
        return super.execute(sqlMap);
    }

}

我从这个方法调用这个类。

 public int incrementExtraBalance(RateLimitLogBean inputBean) {
    IncrementExtraBalanceStoredProcedure procedure = new IncrementExtraBalanceStoredProcedure(this.jdbcTemplate, "RATELIMIT_OWN.increment_extra_balance");
    Map<String, Object> resultMap = procedure.execute(inputBean);
    if (!StringUtils.isEmpty(resultMap)) {
        return ((Integer) resultMap.get(O_RETURN_CODE)).intValue();
    }
    return -1;
}

但我得到空值作为 O_RETURN_CODE。它应该返回 0 从 Toad 执行此函数 - Oracle Db

变量 z 数

执行 RATELIMIT_OWN.unlimit_contract (0123,:z)

打印 z

我在 Toad 中得到 0 作为输出。

为什么我从 Java 代码返回 null 值(没有 sql 异常)。代码有什么问题吗?

本机调用返回正确的输出

public void unlimitContract(RateLimitLogBean inputBean, boolean load) throws SQLException {
    String sql = "{call RATELIMIT_OWN.unlimit_contract (?,?)}";
    CallableStatement callableStatement = this.dataSource.getConnection().prepareCall(sql);
    callableStatement.setInt(1, 0123);
    callableStatement.registerOutParameter(2, java.sql.Types.INTEGER);
     
    callableStatement.executeUpdate();
     
    int resultCode = callableStatement.getInt(2);
}

SQL SP

CREATE OR REPLACE PROCEDURE RATELIMIT_OWN.increment_extra_balance (p_numec IN  NUMBER,
  p_gbytes            IN    NUMBER,
  o_return_code    OUT NUMBER)
AS
   
   message            logs.errormsg%TYPE;
   
BEGIN

  update balance set extrabalance=extrabalance+(p_gbytes*1073741824),limited=0 WHERE numec = p_numec;
  
  IF SQL%ROWCOUNT = 0
  THEN
    o_return_code:=1;
  ELSE
    o_return_code:=0;
    message := 'Cops added ' || p_gbytes || ' gb extra volume';
    INSERT INTO logs (logid, eventid, origin, numec, VALUE, errormsg) VALUES (seq_log.NEXTVAL, 'NEXTRAROV', 'increment_extra_balance', p_numec, p_gbytes, message);
  END IF;

   commit;
  
  EXCEPTION
    WHEN OTHERS
    THEN
      o_return_code := SQLCODE;
      ROLLBACK;  
        
END;
/
4

1 回答 1

1

你的参数顺序看起来不对。尝试:

   declareParameter(new SqlParameter(P_NUMEC, Types.INTEGER));
   declareParameter(new SqlParameter(P_GBYTES, Types.INTEGER));
   declareParameter(new SqlOutParameter(O_RETURN_CODE, Types.INTEGER));
于 2012-09-12T10:02:01.087 回答