Optimal way to pass values from callable parameters in java

Viewed 208

I am looking for the best practice about passing values from a callable parameter object to separate variables.

I am using an object of callable parameters to pass values to variables. I am using numbers to represent the values of every parameter. Is there a more efficient way?

//CREATES NEW OBJECT FOR COLLABLE PARAMETERS
CallableParameters params = new CallableParameters();
//PASS INPUT VALUES
params.addParamIn(ID(),Types.INTEGER); 
params.addParamIn(NUMBER(),Types.INTEGER); 


//RETURN OUTPUT VALUES
params.addParamIn(CITY(),Types.VARCHAR); 
params.addParamIn(YEAR(),Types.INTEGER); 

//EXECUTE THE COLLABLE STATEMENT TO RUN THE STORED PROCEDURE
CallableStatement.executeCallable(callableName, params, conn); 

Then I try to pass the values to variables like this:

String CITY = (Integer)params.getParams().get(3).getParamValue();
int YEAR=(String)params.getParams().get(4).getParamValue();

Is there a better way to call my variables instead of get(3), like enum with names and like?

1 Answers

You can create am enum

enum ParameterSetter implements Consumer<CallableParameters> {
    ID {
        void accept(Callable Parameters setIn) {
            setIn.addParamIn(createId(), Types.INTEGER);
        } 
    }
    static String createId() {...} 
}

Then you can initialize your parameters like this

for (ParameterSetter setter : ParameterSetter.values()) {
    setter.accept(yourParameterObject);
}

Is a matter of taste I guess.

Related