I have a stored procedure which has body like :-
PROCEDURE PROC_NAME(param1 in varchar2,param2 in varchar2,results_cursor OUT CURSOR_TYPE);
Each
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 execute(String param1, String param2) {
Map 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 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 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")