I’m using the JDO Query class and I need to conditionally pass certain parameters to the execute method, as demonstrated in the code below.
I don’t know how to do this in Java.
query.declareParameters("String alphaP");
query.declareParameters("String bravoP");
query.declareParameters("String charlieP");
if (condition) {
if (othercondition) {
query.declareParameters("List<String> stringList");
}
else {
query.declareParameters("String simpleString");
}
}
if (someothercondition) {
query.declareParameters("int deltaP");
}
if (yetanothercondition) {
query.declareParameters("int echoP");
}
if (thelastcondition) {
query.declareParameters("int foxtrotP");
}
List<ResultClass> results = (List<ResultClass>) query.execute(
alphaP,
bravoP,
charlieP,
// ... and whatever other parameters are required based on
// which conditions are true above
);
I would suggest using the
executeWithArraymethod: dynamically build up the array of parameters using an instance ofjava.util.ArrayListwhich you can then convert to an array using thetoArraymethod. The code might look something like the following: