0

I have the following stored procedure which takes three parameters and returns three ref cursors.

variable id refcursor
variable item refcursor
variable amount refcursor
exec getdata(123,date1,date2, :id, :item, :amount) ;

print id;
print item;
print amount;

i have three resultsets for this stored procedure output. How can i call this in spring mvc and display these three resultsets. I was using the following code to fetch the data through sql query. But now i have developed a stored procedure. so how can i call this SP output insted of my query output.

public Optional<List<student>> getStudentDetails(String id) {

NamedParameterJdbcTemplate parameterJdbcTemplate = new 
NamedParameterJdbcTemplate(dataSource);

MapSqlParameterSource namedParameters = new MapSqlParameterSource();
namedParameters.addValue("Id", id);

List<student> studentList = 
parameterJdbcTemplate.query(StudentQueryRepository.STUDENT_DETAIL_QUERY,
namedParameters, new studentDecodeRowMapper());

if (studentList.isEmpty()) {
return Optional.empty();
}   else {
return Optional.of(studentList);
}

}
1
  • please remove the sql-server tag if it's not required Commented Oct 17, 2017 at 7:49

1 Answer 1

0

Try this:

List<CommunicationContact> campaigns = jdbcTemplate.execute(
    new CallableStatementCreator() {
        public CallableStatement createCallableStatement(Connection con) throws SQLException {
            CallableStatement cs = con.prepareCall("{? = call MY_SERVICES.GET_CAMPAIGNS(?,?)}");
            cs.registerOutParameter(1, OracleTypes.CURSOR);
            cs.setString(2, customer);
            cs.setString(3, channel);
            return cs;
        }
    },
    new CallableStatementCallback<List<CommunicationContact>>() {
        @Override
        public List<CommunicationContact> doInCallableStatement(CallableStatement cs) throws SQLException, DataAccessException {
            cs.execute();
            ResultSet rs = (ResultSet) cs.getObject(1);
            List<CommunicationContact> communications = commContactRsExtractor.extractData(rs);
            return communications;
        }
    }
);

And the I have a function on the database:

FUNCTION GET_CAMPAIGNS(p_cust IN VARCHAR2,
                                     p_channel IN VARCHAR2)
    RETURN SYS_REFCURSOR
  IS
    l_campaigns_cursor SYS_REFCURSOR;
    BEGIN
      BEGIN
        OPEN l_campaigns_cursor FOR
        SELECT...

        EXCEPTION
        WHEN OTHERS THEN
        ...
      END;

      RETURN l_campaigns_cursor;
    END GET_CAMPAIGNS;

I believe that a stored procedure with out parameter will also work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.