X-Original-To: pgsql-jdbc-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 1B8183A3E75 for ; Tue, 16 Nov 2004 17:31:46 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 16472-06 for ; Tue, 16 Nov 2004 17:31:38 +0000 (GMT) Received: from outbound-mail.nyc.untd.com (outbound-mail.nyc.untd.com [64.136.20.164]) by svr1.postgresql.org (Postfix) with SMTP id CE4713A3E53 for ; Tue, 16 Nov 2004 17:31:37 +0000 (GMT) Received: from outbound21-sr.nyc.untd.com (webmail25.nyc.untd.com [10.141.27.165]) by smtpout03.nyc.untd.com with SMTP id AABA3WQLAAZQLY22 for (sender ); Tue, 16 Nov 2004 09:30:40 -0800 (PST) X-UNTD-OriginStamp: vtoMeCIkS7uYlJqMM3k7yb56zd/neULxxM0eNO8L4s0muy2imc3bfA== Received: (from enishiseki@juno.com) by webmail25.nyc.untd.com (jqueuemail) id KCKVZM5Y; Tue, 16 Nov 2004 09:30:07 PST Received: from [69.201.176.182] by webmail25.nyc.untd.com with HTTP: Tue, 16 Nov 2004 17:29:23 GMT X-Originating-IP: [69.201.176.182] Mime-Version: 1.0 From: "enishiseki@juno.com" Date: Tue, 16 Nov 2004 17:29:23 GMT To: pgsql-jdbc@postgresql.org Subject: JDBC calling PL/pgSQL with array parameter X-Mailer: WebMail Version 2.0 Content-Type: text/plain Message-Id: <20041116.093007.42.454@webmail25.nyc.untd.com> X-ContentStamp: 16:8:2631430082 X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=2.8 tagged_above=0.0 required=5.0 tests=FORGED_JUNO_RCVD X-Spam-Level: ** X-Archive-Number: 200411/117 X-Sequence-Number: 11539 Hello, I'm trying to call a PL/pgSQL function from a Java program using JDBC. The function takes an integer array as an input parameter, sums the elements of the array, and returns the sum. I can successfully call the function from psql as: SELECT testit('{1,2,3}') However, I cannot get it to run successfully when called from Java. Can anyone give me an example of the JDBC to use? I'm running PostgreSQL 8.0 beta2 on Windows XP. My PL/pgSQL is the following: =============================================================== CREATE OR REPLACE FUNCTION testit(_INT4) RETURNS INTEGER AS $$ ------------------------------------------------------------- DECLARE ------------------------------------------------------------- sum_of_ints INTEGER := 0; ints ALIAS FOR $1; ------------------------------------------------------------- BEGIN ------------------------------------------------------------- FOR i IN 1..3 LOOP sum_of_ints := sum_of_ints + ints[i]; END LOOP; RETURN sum_of_ints; END; $$ LANGUAGE 'plpgsql'; =============================================================== The following is my Java code: =============================================================== import java.sql.*; public class PlSqlTest { public static void main(String args[]) { CallableStatement stmt = null; int result = 0; int [] intArray = {1,2,3}; System.out.println("\nArray Parameter Test\n"); try { Class.forName("org.postgresql.Driver"); Connection conn = DriverManager.getConnection ("jdbc:postgresql://localhost/test1", "postgres", "2adfw2d"); conn.setAutoCommit(false); stmt = conn.prepareCall("{?= call testit(?)}"); stmt.registerOutParameter(1, Types.INTEGER); stmt.setObject(2, intArray ); stmt.execute(); result = stmt.getInt(1); System.out.println("\nThe result is " + result); conn.close(); } catch (SQLException se) { System.out.println("\nA SQL exception occurred."); System.out.println("\nError code: " + se.getErrorCode()); System.out.println("\nSQL state: " + se.getSQLState()); se.printStackTrace(); } catch (Exception e) { System.out.println("\nSome other exception occurred.\n"); e.printStackTrace(); } } } ============================================================= When I try to run the program, I get the error, "missing dimension value". See below: ============================================================ C:\dev\pl\PLpgSQL>java PlSqlTest C:\dev\pl\PLpgSQL>"C:\java\sdk\jrockit-1.4.2_04"\bin\java -classpath ".;c:\dev\j TPCC\lib\pg74.215.jdbc3.jar" PlSqlTest Array Parameter Test A SQL exception occurred. Error code: 0 SQL state: 22P02 org.postgresql.util.PSQLException: ERROR: missing dimension value at org.postgresql.util.PSQLException.parseServerError(Ljava.lang.String; )Lorg.postgresql.util.PSQLException;(PSQLException.java:139) at org.postgresql.core.QueryExecutor.executeV3()Lorg.postgresql.core.Bas eResultSet;(QueryExecutor.java:152) at org.postgresql.core.QueryExecutor.execute()Lorg.postgresql.core.BaseR esultSet;(QueryExecutor.java:100) at org.postgresql.core.QueryExecutor.execute([Ljava.lang.String;[Ljava.l ang.Object;Lorg.postgresql.core.BaseStatement;)Lorg.postgresql.core.BaseResultSe t;(QueryExecutor.java:43) at org.postgresql.jdbc1.AbstractJdbc1Statement.execute()Z(AbstractJdbc1S tatement.java:517) at org.postgresql.jdbc2.AbstractJdbc2Statement.execute()Z(AbstractJdbc2S tatement.java:50) at PlSqlTest.main([Ljava.lang.String;)V(PlSqlTest.java:18) C:\dev\pl\PLpgSQL> ============================================================== Thanks, Ernie Nishiseki ________________________________________________________________ Juno Platinum $9.95. Juno SpeedBand $14.95. Sign up for Juno Today at http://www.juno.com! Look for special offers at Best Buy stores.