How to call a stored procedure with ref cursor as an output parameter using Spring?
Asked Answered
G

2

4

I have a stored procedure which has body like :-

PROCEDURE PROC_NAME(param1 in varchar2,param2 in varchar2,results_cursor OUT CURSOR_TYPE);

Each row of result is equivalent to an instance of a certain user defined class.

How can I call this in Spring. I went through lot of google and stackoverflow but could not find an apt answer.

Can anyone please provide me a solution to this. Thanks in advance.

Gamekeeper answered 27/3, 2017 at 11:22 Comment(3)
I'm not sure how to do it with JdbcTemplate but I've done it using plain JDBC. I can post some code if you can use JDBC.Piperpiperaceous
@dsp_user: I cannot use JDBC in my code.Gamekeeper
Well, JDBCTemplate already uses it internally but it's really up to you.Piperpiperaceous
I
5

Here's something I put together based on this StackOverflow question and the Spring documentation:

import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import oracle.jdbc.OracleTypes;
import org.springframework.jdbc.core.SqlOutParameter;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.object.StoredProcedure;

public class SampleStoredProcedure extends StoredProcedure {

    public SampleStoredProcedure(DataSource dataSource) {
        super(dataSource, "PROC_NAME");
        declareParameter(new SqlParameter("param1", Types.VARCHAR));
        declareParameter(new SqlParameter("param2", Types.VARCHAR));
        declareParameter(new SqlOutParameter("results_cursor", OracleTypes.CURSOR, new SomeRowMapper()));
        compile();
    }

    public Map<String, Object> execute(String param1, String param2) {
        Map<String, Object> inParams = new HashMap<>();
        inParams.put("param1", param1);
        inParams.put("param2", param2);
        Map output = execute(inParams);
        return output;
    }
}

If your stored procedure is in another schema or in a package, you'll need to adjust the stored procedure name in the above. Also, you'll need to specify a row mapper to use in place of SomeRowMapper.

To call it:

    DataSource dataSource = ... ; // get this from somewhere
    SampleStoredProcedure sp = new SampleStoredProcedure(dataSource);
    Map<String, Object> result = sp.execute("some string", "some other string");
    // Do something with 'result': in particular, result.get("results_cursor")
    // will be the list of objects returned

Alternatively, you can use a SimpleJdbcCall:

    DataSource dataSource = ... ; // get this from somewhere
    SimpleJdbcCall jdbcCall = new SimpleJdbcCall(dataSource);
    Map<String, Object> result =
        jdbcCall.withProcedureName("PROC_NAME")
            .declareParameters(
                    new SqlParameter("param1", Types.VARCHAR),
                    new SqlParameter("param2", Types.VARCHAR),
                    new SqlOutParameter("results_cursor", OracleTypes.CURSOR, new SomeRowMapper()))
            .execute("some string", "some other string");

If the stored procedure is in a package, you'll need to add a line

            .withCatalogName("PACKAGE_NAME")

to the setup of jdbcCall. Similarly, if it's in a different schema, you'll need to add

            .withSchemaName("SCHEMA_NAME")
Incompetence answered 27/3, 2017 at 14:30 Comment(1)
can you share sample implmentation of SomeRowMapper alsoAperture
S
2

Please have look a look once. List userData;

    SimpleJdbcCall  executor = new SimpleJdbcCall(jdbcTemplate)
                                .withProcedureName("SP_CL_USERPKS_FOLDER").withoutProcedureColumnMetaDataAccess()
                                .declareParameters(
                                    new SqlParameter("INparam1", Types.INTEGER),
                                    new SqlParameter("INparam2", Types.VARCHAR),
                                    new SqlOutParameter("OUTParam1", OracleTypes.CURSOR),
                                    new SqlOutParameter("OUTParam2", OracleTypes.VARCHAR));
    executor.compile();
    SqlParameterSource param = new MapSqlParameterSource()
            .addValue("INparam1", loginPk)
            .addValue("INparam2", email);

    Map map = executor.execute(param);
    userData = (List<Map>) map.get("OUTParam1");
    String msg = (String) map.get("OUTParam2");
Shrove answered 28/8, 2018 at 6:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.