I would like to call a stored procedure (Oracle). As I my procedure calls are dynamical, I need to manage the procedures parameters dynamically.
The problem is that I would like to avoid using the instanceof keyword since it does not work well with primary types.
Is there a cleaner way to call a such procedure?
public void executeProcedure(StoredProcedure proc) {
LinkedList<ProcedureParameter<?>> parameters = proc.getParameters();
CallableStatement statement = connection.prepareCall("{ call MY_PACKAGE.MY_PROCEDURE( ?, ?, ? ) }");
for (int i = 0; i < parameters.size(); i++) {
Object paramValue = parameters.get(i).getValue();
// Beginning of smelly code
if (paramValue instanceof String) {
statement.setString(i + 1, (String) paramValue);
}
else if (paramValue instanceof ...) {
...
}
else {
...
}
// End of smelly code
}
statement.execute();
}
public class ProcedureParameter<E> {
private String name;
private E value;
public ProcedureParameter(String name) {
this.name = name;
}
public ProcedureParameter(String name, E value) {
this.name = name;
this.value = value;
}
// Getters and setters
}
Thank you for any idea.
I will implement another solution, that is probably not be best one, but in my opinion, not smelly though.
As
StoredProcedureis an interface, each stored procedure implements this interface. Thus, each implementation will contain a method that returns thecall ...statement, as the developer knows the type of the parameters needed for the procedure he implements.