带命名参数的spring CallableStatement

时间:2016-08-25 13:56:54

标签: java spring stored-procedures plsql

当我尝试使用parameterIndex(setInt(1,id))设置输入时,它工作得很好。但是当我尝试使用parametername(如下面的代码)id时,它返回null值,我不明白为什么。有人可以帮忙吗?

我的程序是:

create or replace PROCEDURE PROCEDURE2 (
   i_emp_no IN INT  default 1234,
   o_emp_name OUT VARCHAR2,
   out_sal OUT INT)
AS

BEGIN
  o_emp_name := NULL;
  out_sal := NULL;

  SELECT ENAME,SAL 
  INTO o_emp_name, out_sal  
  FROM EMP 
  WHERE EMPNO = i_emp_no;

END PROCEDURE2;

我从春天的召唤方法是:

public ResponseModel testProcedure(int id) throws SQLException{

    Connection dbConnection = null;
    CallableStatement callableStatement = null;

    String procedureCallString = "{call PROCEDURE2(?,?,?)}";
    ResponseModel response = new ResponseModel();

    try {           
        if(dataSource == null){
            logger.debug("dataSource is null");
            return response;
        }

        dbConnection = dataSource.getConnection();
        callableStatement = dbConnection.prepareCall(procedureCallString);

        callableStatement.setInt("i_emp_no", id);
        callableStatement.registerOutParameter(2, java.sql.Types.VARCHAR);
        callableStatement.registerOutParameter(3, java.sql.Types.INTEGER);

        boolean isResultSet = callableStatement.execute();

        if ( isResultSet ){             
            logger.debug("returned resultset");

        }else{
            logger.debug("NOT returned resultset");             
        }

        response.setName(callableStatement.getString(2));
        response.setAddress("address");
        response.setHeight(callableStatement.getInt(3));

        //System.out.println("enmp name : " + userName);
        //System.out.println("sal : " + sal);

    } catch (SQLException e) {
        System.out.println(e.getMessage());

    }finally {

        if (callableStatement != null) {
            callableStatement.close();
        }

        if (dbConnection != null) {
            dbConnection.close();
        }
    }

    return response;        
}

0 个答案:

没有答案