1

Hi I am trying to execute stored procedures using Spring JDBC. Here's the SP class

 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);
    }

}

I am calling this class from this method.

 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;
}

But I am getting null value as O_RETURN_CODE. It's supposed to return 0 The execution of this function from Toad - Oracle Db

var z number

exec RATELIMIT_OWN.unlimit_contract (0123,:z)

print z

I got 0 as output in Toad.

Why I am getting null value as return from Java code (no sql exceptions). Is there anything wrong with code?

native calls returning proper output

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;
/
Community
  • 1
  • 1
RaceBase
  • 18,428
  • 47
  • 141
  • 202

1 Answers1

1

The order of your parameters looks wrong. Try:

   declareParameter(new SqlParameter(P_NUMEC, Types.INTEGER));
   declareParameter(new SqlParameter(P_GBYTES, Types.INTEGER));
   declareParameter(new SqlOutParameter(O_RETURN_CODE, Types.INTEGER));
pd40
  • 3,187
  • 3
  • 20
  • 29
  • 1
    Really thanks. that helped lot. It was one hell of silly mistake that took one day for me – RaceBase Sep 12 '12 at 12:17
  • can you please look at this one also http://stackoverflow.com/questions/12388626/executing-oracle-functions-using-spring-jdbc – RaceBase Sep 12 '12 at 12:53