pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>
To: JavaNoobie <vivek.mv@enzentech.com>
Cc: pgsql-jdbc@postgresql.org
Subject: Re: CallableStatement PostgreSQL Error:A CallableStatement was executed with an invalid number of parameters
Date: Thu, 24 Nov 2011 21:40:30 +0200
Message-ID: <4ECE9DAE.9050301@enterprisedb.com> (raw)
In-Reply-To: <1322107988531-5019196.post@n5.nabble.com>
References: <1322107988531-5019196.post@n5.nabble.com>

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



view thread (3+ messages)  latest in thread

Message-ID: <4ECE9DAE.9050301@enterprisedb.com>
Permalink:  ../4ECE9DAE.9050301@enterprisedb.com/
Also on:    postgresql.org/message-id/4ECE9DAE.9050301@enterprisedb.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: heikki.linnakangas@enterprisedb.com, vivek.mv@enzentech.com
  Subject: Re: CallableStatement PostgreSQL Error:A CallableStatement was executed with an invalid number of parameters
  In-Reply-To: <4ECE9DAE.9050301@enterprisedb.com>
  References: <1322107988531-5019196.post@n5.nabble.com> <4ECE9DAE.9050301@enterprisedb.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox