BobbyDropTables
BobbyDropTables

Reputation: 63

Passing a multidimensional array as a literal to a method call in Java

I have written two convenience methods for accessing a more complex method for parameterized sql queries/executes.

The first one is:

public int sqlExecute(String sql, String... parameters)

which i can call like

sqlExecute("exec blah ?, ?, ?", "param1", "param2", "param3");

The second one is defined as:

public int sqlExecute(String sql, String[]... parameters)

which should be called with an array[2][whatever size] that holds in the first column the type (more precisely its the "table.column" of a field which provides the type... I have to implement it this way) and the second the value of the parameter to use.

A call like

sqlExecute("exec blah ?, ?", {"account.name", "foo"}, {"account.region", "bar"});

results in "Syntax error on tokens, delete these tokens" (using Eclipse Luna). Isn't that correct syntax?

What I wish to accomplish is that I don't have to define some variable which holds the array, but rather call the method with an array literal like I can with method #1. Is there another way?

Upvotes: 1

Views: 211

Answers (2)

Tagir Valeev
Tagir Valeev

Reputation: 100279

You should call it like

sqlExecute("exec blah ?, ?", 
    new String[] {"account.name", "foo"}, 
    new String[] {"account.region", "bar"});

You can add a helper method to make this shorter:

public static String[] arr(String... args) {
    return args;
}

sqlExecute("exec blah ?, ?", 
    arr("account.name", "foo"), 
    arr("account.region", "bar"));

Upvotes: 4

Kayaman
Kayaman

Reputation: 73568

You need to use

sqlExecute("exec blah ?, ?",
        new String[] {"account.name", "foo"},
        new String[] {"account.region", "bar"});

Upvotes: 4

Related Questions