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 094D23A5DF7 for ; Sun, 5 Dec 2004 19:36:59 +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 54775-02 for ; Sun, 5 Dec 2004 19:36:56 +0000 (GMT) Received: from wproxy.gmail.com (wproxy.gmail.com [64.233.184.199]) by svr1.postgresql.org (Postfix) with ESMTP id 6C39D3A5DF3 for ; Sun, 5 Dec 2004 19:36:56 +0000 (GMT) Received: by wproxy.gmail.com with SMTP id 69so34469wri for ; Sun, 05 Dec 2004 11:36:56 -0800 (PST) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:reply-to:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:references; b=h45E6cBME3o2UfA5gS1gGojCRdlGyQuCVwHmGJv1DhAle84wVsM+DGXE00TWPzM97OOPGF2c4+eXVK9r5GhdRwMoSi71er0+W0g+wnbnKXYN+uSAG6B33o8rN5a48axHcG4V5C/MmSXTuARlpf0iTuVr4gBfPDF0BPX/TIF5DWw= Received: by 10.54.49.71 with SMTP id w71mr1071953wrw; Sun, 05 Dec 2004 11:36:55 -0800 (PST) Received: by 10.54.53.13 with HTTP; Sun, 5 Dec 2004 11:36:54 -0800 (PST) Message-ID: Date: Sun, 5 Dec 2004 14:36:54 -0500 From: Larry White Reply-To: Larry White To: Thomas Hallgren Subject: Re: prepared statement call fails Cc: pgsql-jdbc@postgresql.org In-Reply-To: <41B35CC6.2030800@mailblocks.com> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit References: <41B35CC6.2030800@mailblocks.com> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.54 tagged_above=0 required=5 tests=FROM_ENDS_IN_NUMS, RCVD_BY_IP X-Spam-Level: X-Archive-Number: 200412/22 X-Sequence-Number: 11688 Thanks Thomas. I'll try it your way to see what happens, but according to the Postgresql documentation, it should support callable statements. I posted the relevent text from the JDBC section of the online docs below: PostgreSQL's JDBC driver fully supports calling PostgreSQL stored functions. Example 31-4. Calling a built in stored function This example shows how to call a PostgreSQL built in function, upper, which simply converts the supplied string argument to uppercase. // Turn transactions off. con.setAutoCommit(false); // Procedure call. CallableStatement upperProc = con.prepareCall("{ ? = call upper( ? ) }"); upperProc.registerOutParameter(1, Types.VARCHAR); upperProc.setString(2, "lowercase to uppercase"); upperProc.execute(); String upperCased = upperProc.getString(1); upperProc.close(); On Sun, 05 Dec 2004 20:08:54 +0100, Thomas Hallgren wrote: > Larry White wrote: > > > > I'm hoping someone with more experience can help me find a problem in > > calling a function from Java. This is the first time I'm trying this > > so I'm guessing it will be straightforward. > > > > The function's signature is: > > > > add_country(bigint, char(2), char(3), varchar(60)) > > RETURNS INTEGER ' > > > > It works fine if I call it from the command line, like so... > > > > select add_country(124,'US', 'USA', 'United States of America'); > > > > In java, I call it using the following: > > > > CallableStatement proc = null; > > proc = connection.prepareCall("{ ? = call add_country( ? ? ? ? ) }"); > > proc.registerOutParameter(1, java.sql.Types.INTEGER); > > proc.setInt(2, 124); > > proc.setString(3, code2); // a two character java String > > proc.setString(4, code3); // a three character java String > > proc.setString(5, name); // a Java String > > proc.execute(); > > > AFAIK, CallableStatement is for stored procedure calls. Stored > procedures are not yet implemented in PostgreSQL. It only has functions. > In order to call a function you need a select statement and a normal > PreparedStatement. Try this: > > PreparedStatement stmt = connection.prepareStatement("select > add_country(?,?,?,?)"); > stmt.setInt(1, 124); > stmt.setString(2, code2); > stmt.setString(3, code3); > stmt.setString(4, code4); > ResultSet rs = stmt.executeQuery(); > if(rs.next()) > result = rs.getInt(1); > > Kind regards, > Thomas Hallgren > > ---------------------------(end of broadcast)--------------------------- > TIP 2: you can get off all lists at once with the unregister command > (send "unregister YourEmailAddressHere" to majordomo@postgresql.org) >