Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 987
Sharda Prasad Jaiswal Avatar asked Sep 06 '25 03:09

Sharda Prasad Jaiswal


1 Answers

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")
like image 104
Luke Woodward Avatar answered Sep 07 '25 18:09

Luke Woodward