Calling Oracle procedure that returns rows using S

2019-08-20 07:39发布

问题:

I have written the following code

       MapSqlParameterSource in = new MapSqlParameterSource();
       in.addValue("V_OPP_ID", bean.getOpportunityId());
       in.addValue("V_NAME",bean.getName());
       in.addValue("V_FROM_DATE", bean.getStdate());
       in.addValue("V_TO_DATE", bean.getEddate());
       in.addValue("V_USERTYPE", bean.getUserType());
       jdbcCall.execute(in);

Here the jdbcCall.execute(in) returns me resultset/table corresponding to Arraylist. How do i extract this ArrayList

Is using jdbcCall a correct Approach ? If not what is Adviced ?

回答1:

This is the code I use for a call to a function:

RowMapper<String> rm = new ParameterizedRowMapper<String>() {
    @Override
    public String mapRow(ResultSet rs, int rowNum) throws SQLException {
        return rs.getString(1);
    }
};
myStoredProcedure = new SimpleJdbcCall(DataSourceConnection.getDataSource())
        .withCatalogName("PACKAGE")
        .withFunctionName("GET_ALIAS")
        .returningResultSet("return", rm);

MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("P_ID",userStr);
params.addValue("P_DOMAIN_ALIAS", domain[0]);
List<String> list = myStoredProcedure.executeFunction(List.class,params);

and if you are not able to use the metadata then this is the code:

RowMapper<String> rm = new ParameterizedRowMapper<String>() {
    @Override
    public String mapRow(ResultSet rs, int rowNum) throws SQLException {
        return rs.getString(1);
    }
};
SqlParameter emailParam = new SqlParameter("P_ID", OracleTypes.VARCHAR);
SqlParameter domainParam = new SqlParameter("P_DOMAIN_ALIAS", OracleTypes.VARCHAR);
SqlOutParameter resultParam = new SqlOutParameter("return", OracleTypes.CURSOR);
myStoredProcedure = new SimpleJdbcCall(DataSourceConnection.getDataSource())
        .withCatalogName("PACKAGE")
        .withFunctionName("GET_ALIAS")
        .withoutProcedureColumnMetaDataAccess()
        .returningResultSet("return", rm)
        .declareParameters(resultParam, emailParam, domainParam);

MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("P_ID",userStr);
params.addValue("P_DOMAIN_ALIAS", domain[0]);
List<String> list = myStoredProcedure.executeFunction(List.class,params);