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 99C29D1B73D for ; Fri, 18 Jun 2004 02:47:38 -0300 (ADT) 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 28007-03 for ; Fri, 18 Jun 2004 05:47:27 +0000 (GMT) Received: from mail63.csoft.net (unknown [63.111.22.74]) by svr1.postgresql.org (Postfix) with SMTP id C2DA7D1B33D for ; Fri, 18 Jun 2004 02:47:18 -0300 (ADT) Received: (qmail 16228 invoked by uid 1112); 18 Jun 2004 05:47:19 -0000 Date: Fri, 18 Jun 2004 00:47:19 -0500 (EST) From: Kris Jurka X-X-Sender: books@leary.csoft.net To: Jie Liang Cc: pgsql-sql@postgresql.org, pgsql-jdbc@postgresql.org Subject: Re: Prepare Statement In-Reply-To: Message-ID: References: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200406/84 X-Sequence-Number: 10286 On Thu, 17 Jun 2004, Jie Liang wrote: > Kris, > I have another question, I saw some discussion regarding > PreparedStatement work with array argument, I get a error when I try to > play with it. > E.g. > I have myfunction(int[]), > So, > PrepareStatement st = conn.prepareStatment("SELECT myfunction(?)"); > String arr="{1,2,3}"; > St.setString(1,arr}; > Result rs = st.executeQuery(); > > Then it will complaint when it run: > Myfuntion(text) does not exist! > This is actually a case where prepared statements actually cause trouble. With the directly executed SELECT myfunction('{1,2,3}'); The backend can determine that there is only one version of myfunction so it can convert the unkown argument type to it, but note that this won't work if myfunction is overloaded. With the prepared case, you must tell it what types to use when doing the prepare. The JDBC driver doesn't have a whole lot of information to work with, so it takes what it knows (that you called setString) and says the argument is of type text, issuing a prepare like this: PREPARE JDBC_STATEMENT_1(text) AS SELECT myfunction($1); At this time (before it actually calls EXECUTE) it tries to lookup myfunction that takes a text argument and determines there isn't one. In this case it doesn't have the opportunity to apply any casts because we were quite clear in specifying that it should take a text argument, not one of unknown type. Ideally you should be using setArray, but there is no existing way to create Array objects and I'm not sure that code would work even if there was. Kris Jurka