Calling Oracle procedure that returns rows using SimpleJdbcCall in Spring

谁说我不能喝 提交于 2019-12-25 05:13:26

问题


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


来源:https://stackoverflow.com/questions/18865531/calling-oracle-procedure-that-returns-rows-using-simplejdbccall-in-spring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!