将java整数数组作为postgres过程的参数传递

时间:2017-05-20 05:02:38

标签: java sql arrays database postgresql

定义java整数数组,如下所示,

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

然后使用连接对象将java数组转换为postgres类型数组,如下所示

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

之后传递 dArray 作为postgres程序的参数,如下所示,

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

我写了postgres程序签名,如下所示,

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

但是当我运行java代码时,它会抛出格式错误的程序/函数调用......

有人建议我解决这个问题。

提前致谢。

1 个答案:

答案 0 :(得分:1)

Hełm,您应该使用Method设置Sql语句的参数,而不是在sql String中。 尝试这样的事情:

        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();