Question

I have a procedure in Oracle that has 12 total parameters and 3 optional. How can I account for these optional parameters if I'm using the StoredProcedure object in Spring 3.1.0?

Here's what I have so far in my StoredProcedure class

public Map<String, Object> execute(Evaluation evaluation) {
    Map<String, Object> input_params = new HashMap<String, Object>();
    input_params.put(COURSE_MAIN_PK1_INPUT_PARAM, evaluation.getCourseId());
    input_params.put(USERS_PK1_INPUT_PARAM, evaluation.getUsersPk1());
    input_params.put(ACCREDITATION_PK1_INPUT_PARAM, evaluation.getAccreditationPk1());
    input_params.put(TYPE_PK1_INPUT_PARAM, evaluation.getTypePk1());
    input_params.put(PRIVACY_TYPE_PK1_INPUT_PARAM, evaluation.getPrivacyTypePk1());
    input_params.put(FORM_TYPE_PK1_INPUT_PARAM, evaluation.getFormTypePk1());
    input_params.put(TITLE_INPUT_PARAM, evaluation.getTitle());
    input_params.put(DESCRIPTION_INPUT_PARAM, evaluation.getDescription());

    if(evaluation.getStartDate() != null) {
        input_params.put(START_DATE_INPUT_PARAM, new java.sql.Date(evaluation.getStartDate().getMillis()));         
    }

    if(evaluation.getEndDate() != null) {
        input_params.put(END_DATE_INPUT_PARAM, new java.sql.Date(evaluation.getEndDate().getMillis())); 
    }

    input_params.put(SAVE_TO_GRADECENTER_INPUT_PARAM, evaluation.getGradeCenterColumn());
    input_params.put(CREATE_ANNOUNCEMENT_INPUT_PARAM, evaluation.getAnnouncement());

    return super.execute(input_params);
}

The problem with this is that I'm supplying 12 parameters and if the start and end dates are null, now I'm supplying 10 and getting an exception.

The default values for the dates in the database is null.

Was it helpful?

Solution

JDBC's PreparedStatement provides a facility to set null values to parameters using the setNull method. So, as long as you pass all the parameters to the stored procedure if they are null, Spring would be able to prepare the statement and execute it.

So, you needed to add the input parameters whose values are null, to the Map that is sent to the stored procedure call.

input_params.put(START_DATE_INPUT_PARAM, 
                 (null != evaluation.getStartDate() 
                  ? new java.sql.Date(evaluation.getStartDate().getMillis()) 
                  : null));

The same would apply to END_DATE_INPUT_PARAM as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top