Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id EC21B2EBA7B for ; Thu, 24 Nov 2011 15:41:13 -0400 (AST) Received: from exprod7og123.obsmtp.com ([64.18.2.24]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1RTfAU-0004fs-Vg for pgsql-jdbc@postgresql.org; Thu, 24 Nov 2011 19:41:13 +0000 Received: from mail-bw0-f41.google.com ([209.85.214.41]) (using TLSv1) by exprod7ob123.postini.com ([64.18.6.12]) with SMTP ID DSNKTs6dyNSN0MOkrsEJ3jMZV34dbpCyO9Or@postini.com; Thu, 24 Nov 2011 11:41:10 PST Received: by bke17 with SMTP id 17so3845624bke.0 for ; Thu, 24 Nov 2011 11:40:55 -0800 (PST) Received: by 10.204.149.215 with SMTP id u23mr31120098bkv.105.1322163633232; Thu, 24 Nov 2011 11:40:33 -0800 (PST) Received: from [192.168.1.100] (dsl-hkibrasgw2-ff6cc300-200.dhcp.inet.fi. [88.195.108.200]) by mx.google.com with ESMTPS id j4sm26232703fae.3.2011.11.24.11.40.31 (version=TLSv1/SSLv3 cipher=OTHER); Thu, 24 Nov 2011 11:40:32 -0800 (PST) Message-ID: <4ECE9DAE.9050301@enterprisedb.com> Date: Thu, 24 Nov 2011 21:40:30 +0200 From: Heikki Linnakangas User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; fi-FI; rv:1.9.2.23) Gecko/20111010 Icedove/3.1.15 MIME-Version: 1.0 To: JavaNoobie CC: pgsql-jdbc@postgresql.org Subject: Re: CallableStatement PostgreSQL Error:A CallableStatement was executed with an invalid number of parameters References: <1322107988531-5019196.post@n5.nabble.com> In-Reply-To: <1322107988531-5019196.post@n5.nabble.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Archive-Number: 201111/90 X-Sequence-Number: 22425 On 24.11.2011 06:13, JavaNoobie wrote: > Im trying to write sample stored functions in postgresql and call them using > the CallableStatement offered by JDBC. > > Here's some my test code > [code=java] > Consumer bean =new Consumer(); > CallableStatement pstmt = null; > try { > con.setAutoCommit(false); > String query = "{ ? = call getData( ? ) }"; > pstmt = con.prepareCall(query); > pstmt.registerOutParameter(1, Types.OTHER); > pstmt.setInt(2,5); > pstmt.execute(); // execute update statement > bean=(Consumer)pstmt.getObject(1); > System.out.println("bean"+bean.getConsumer_name()); > ..... > [/code] > And my Stored function is of the form . > [code=sql] > CREATE FUNCTION getData(int) RETURNS SETOF db_consumer AS $$ > SELECT * FROM db_consumer WHERE consumer_id = $1; > $$ LANGUAGE SQL; > [/code] > > However, I'm getting the following error when I try to run the code . > > [code=java] > org.postgresql.util.PSQLException: A CallableStatement was executed with an > invalid number of parameters > at > org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:408) > at > org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:381) > at com.enzen.cis.dao.Updatetest.main(Updatetest.java:44) > > [/code] > Any idea on why this is happening would be appreciated. Since it's a set-returning function, I think you need to execute it like a query, instead of using CallableStatement. Something like: pstmt = conn.prepareStatement("SELECT getData(?)"); pstmt.setInt(1, 5); rs = pstmt.executeQuery(); -- Heikki Linnakangas EnterpriseDB http://www.enterprisedb.com