Parthiban
Parthiban

Reputation: 105

pass java integer array as arguments to postgres procedure

Defined java integer array as below,

Integer[] details = {5097, 5098, 5099, 5100, 5101, 5102, 5121, 5122, 5123, 5124, 5125, 5126, 5127, 5128, 5129, 5130};

Then converted java array to postgres type array using connection object as below,

connection = (Connection) DriverManager.getConnection(MySQLURL,"postgres","admin");
Array dArray = connection.createArrayOf("INT", details);

After that passed dArray as argument to postgres procedure as below,

String simpleProc = "{ call SMS.P_DELETE_DATA("+dArray+") }";
        CallableStatement cs = connection.prepareCall(simpleProc);
        cs.execute();

I wrote postgres procedure signature as below,

CREATE FUNCTION SMS.P_DELETE_WORKPLAN_FOR_LESSON(P_LESSONS INT[]) 
 RETURNS VOID AS $$

But when i run the java code it throws Malformed procedure / function call ......

Some one advice me to sort out this issue.

Thanks in advance.

Upvotes: 0

Views: 2537

Answers (1)

k_rams
k_rams

Reputation: 76

Hełm, you should set the Parameter for Sql Statement with a Method and not in the sql String. Try something like this:

        connection = (Connection) DriverManager.getConnection(MySQLURL,"postgres","admin");
        Array dArray = connection.createArrayOf("INT", details);
        String simpleProc = "{ call SMS.P_DELETE_DATA(?) }";
        CallableStatement cs = connection.prepareCall(simpleProc);
        cs.setArray(1, dArray);
        cs.execute();
        cs.close();

Upvotes: 2

Related Questions