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 1A03AD1B747 for ; Fri, 30 Jul 2004 01:19:09 -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 09292-07 for ; Fri, 30 Jul 2004 04:18:44 +0000 (GMT) Received: from web90007.mail.scd.yahoo.com (web90007.mail.scd.yahoo.com [66.218.94.65]) by svr1.postgresql.org (Postfix) with SMTP id 49B0FD1B53F for ; Fri, 30 Jul 2004 01:18:24 -0300 (ADT) Message-ID: <20040730041823.98636.qmail@web90007.mail.scd.yahoo.com> Received: from [219.95.203.143] by web90007.mail.scd.yahoo.com via HTTP; Thu, 29 Jul 2004 21:18:23 PDT Date: Thu, 29 Jul 2004 21:18:23 -0700 (PDT) From: Nick Selva Subject: Calling PGSQL Stored function thru JDBC To: pgsql-jdbc@postgresql.org MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="0-595423857-1091161103=:92437" X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.6 tagged_above=0.0 required=5.0 tests=FORGED_YAHOO_RCVD, RCVD_IN_RFCI X-Spam-Level: X-Archive-Number: 200407/333 X-Sequence-Number: 10717 --0-595423857-1091161103=:92437 Content-Type: text/plain; charset=us-ascii Content-Id: Content-Disposition: inline Hi everyone. I'm new here. Need the help of Guru(s) here. I am trying to write some java code to leverage the power of stored-functions is pgsql. I have created the function in the database and tested it with a 'SELECT' function and it seems to return the result requested. But when I try to embed some code into my Java program, it gives errors. I will list out everything below. The stored function that i created in the database looks like this: ------------------------------------------------------- CREATE FUNCTION get_state(char(1)) RETURNS varchar(20) AS ' DECLARE my_state VARCHAR(20); BEGIN SELECT INTO my_state name FROM statename WHERE code=$1; RETURN my_state; END; ' LANGUAGE plpgsql; ------------------------------------------------------- I tested it in the database using the SELECT function as below: ttms=#SELECT get_state('D'); get_state ----------- Dallas (1 row) So this proves that as far as postgresql database is concerned its working perfectly fine. Then I wrote the some java code to read a value from the table using JDBC ResultSet to test if my connection thru JDBC works. And it does. So at the end of the same program I included Callable statemenet code to call the function get_state() from the program to receive the return value. I have attached the program with this email. The file is called as sample.java. Please do review my novice code. I can compile and even run the bytecode. But it only runs without any errors until it reaches the CallableStatement portion. Then the program ends abrubtly catching exception. The following is the error message. --------------------------------------------------------------------------------------------------- Exception caught. org.postgresql.util.PSQLException: PostgreSQL only supports function return value [@ 1] (no OUT or INOUT arguments) org.postgresql.util.PSQLException: PostgreSQL only supports function return value [@ 1] (no OUT or INOUT arguments) at org.postgresql.jdbc1.AbstractJdbc1Statement.registerOutParameter(AbstractJdbc1Statement.java:1745) at sample.(sample.java:41) at sample.main(sample.java:55) --------------------------------------------------------------------------------------------------- I don't think so it's the problam with the JDBC driver, coz if it was, it wouldn't succeed in getting the values for the ResultSet in the earlier portion of the code. I suppose I am missing out something in the syntax or in the code. Anyone have come across this? Please don't tell me jdbc cant work with pgsql stored function coz im planning to write half the application logic using stored function. Somebody please shed some light in this matter. I thank in advance for the help rendered. Will wait for the reply. Best Regards, Selvam __________________________________ Do you Yahoo!? New and Improved Yahoo! Mail - 100MB free storage! http://promotions.yahoo.com/new_mail --0-595423857-1091161103=:92437 Content-Type: application/octet-stream; name="sample.java" Content-Transfer-Encoding: base64 Content-Description: sample.java Content-Disposition: attachment; filename="sample.java" LyoKICAgICAgICAgKiAgSmF2YSBzYW1wbGUgcHJvZ3JhbQogICAgICAgICAq LwogICAgICAgICAKICAgICAgICBpbXBvcnQgamF2YS5pby4qOwogICAgICAg IGltcG9ydCBqYXZhLnNxbC4qOwogICAgICAgICAKICAgICAgICBwdWJsaWMg Y2xhc3Mgc2FtcGxlCiAgICAgICAgewogICAgICAgICAgICBDb25uZWN0aW9u ICBjb25uOyAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgLy8g aG9sZHMgZGF0YWJhc2UgY29ubmVjdGlvbgogICAgICAgICAgICBTdGF0ZW1l bnQgICBzdG10OyAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg Ly8gaG9sZHMgU1FMIHN0YXRlbWVudAogICAgICAgICAgICBTdHJpbmcgICAg ICBzdGF0ZV9jb2RlOyAgICAgICAgICAgICAgICAgICAgICAgICAgICAgLy8g aG9sZHMgc3RhdGUgY29kZSBlbnRlcmVkIGJ5IHVzZXIKICAgICAgICAgCiAg ICAgICAgICAgIHB1YmxpYyBzYW1wbGUoKSB0aHJvd3MgQ2xhc3NOb3RGb3Vu ZEV4Y2VwdGlvbiwgRmlsZU5vdEZvdW5kRXhjZXB0aW9uLCBJT0V4Y2VwdGlv biwgU1FMRXhjZXB0aW9uCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAg IENsYXNzLmZvck5hbWUoIm9yZy5wb3N0Z3Jlc3FsLkRyaXZlciIpOyAgICAg ICAgIC8vIGxvYWQgZGF0YWJhc2UgaW50ZXJmYWNlCiAgICAgICAgICAgICAg ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg ICAgICAvLyBjb25uZWN0IHRvIHRoZSBkYXRhYmFzZQogICAgICAgICAgICAg ICBjb25uID0gRHJpdmVyTWFuYWdlci5nZXRDb25uZWN0aW9uKCJqZGJjOnBv c3RncmVzcWw6Ly9sb2NhbGhvc3Q6NTQzMi90dG1zIiwgInNlbHZhbSIsICJz ZWx2YW0iKTsgCiAgICAgICAgICAgICAgICBzdG10ID0gY29ubi5jcmVhdGVT dGF0ZW1lbnQoKTsKICAgICAgICAgCiAgICAgICAgICAgICAgICBTeXN0ZW0u b3V0LnByaW50KCJFbnRlciBhIHN0YXRlIGNvZGU6ICAiKTsgICAgICAvLyBw cm9tcHQgdXNlciBmb3IgYSBzdGF0ZSBjb2RlCiAgICAgICAgICAgICAgICBT eXN0ZW0ub3V0LmZsdXNoKCk7CiAgICAgICAgICAgICAgICBCdWZmZXJlZFJl YWRlciByID0gbmV3IEJ1ZmZlcmVkUmVhZGVyKG5ldyBJbnB1dFN0cmVhbVJl YWRlcihTeXN0ZW0uaW4pKTsKICAgICAgICAgICAgICAgIHN0YXRlX2NvZGUg PSByLnJlYWRMaW5lKCk7CiAgICAgICAgIAogICAgICAgICAgICAgICAgUmVz dWx0U2V0IHJlcyA9IHN0bXQuZXhlY3V0ZVF1ZXJ5KCAgICAgICAgICAgICAg Ly8gc2VuZCB0aGUgcXVlcnkKICAgICAgICAgICAgICAgICAgICAiU0VMRUNU IG5hbWUgIiArCiAgICAgICAgICAgICAgICAgICAgIkZST00gc3RhdGVuYW1l ICIgKwogICAgICAgICAgICAgICAgICAgIldIRVJFIGNvZGUgPSAnIiArIHN0 YXRlX2NvZGUgKyAiJyIpOwogICAgICAgICAKICAgICAgICAgICAgICAgIGlm IChyZXMgIT0gbnVsbCkgCiAgICAgICAgICAgICAgICAgICAgd2hpbGUocmVz Lm5leHQoKSkgCiAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAg ICAgICAgICAgICBTdHJpbmcgc3RhdGVfbmFtZSA9IHJlcy5nZXRTdHJpbmco MSk7CiAgICAgICAgICAgICAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnRs bihzdGF0ZV9uYW1lKTsKICAgICAgICAgICAgICAgICAgICB9CgoJCWNvbm4u c2V0QXV0b0NvbW1pdChmYWxzZSk7CgkJQ2FsbGFibGVTdGF0ZW1lbnQgY3Mg PSBjb25uLnByZXBhcmVDYWxsKCJ7ID8gY2FsbCBnZXRfc3RhdGUoID8gKSB9 Iik7CgkJY3Muc2V0U3RyaW5nKDEsICJQIik7CgkJY3MucmVnaXN0ZXJPdXRQ YXJhbWV0ZXIoMiwgVHlwZXMuVkFSQ0hBUik7CgkJY3MuZXhlY3V0ZSgpOwoJ CVN0cmluZyB2YWwgPSBjcy5nZXRTdHJpbmcoMSk7CgkJU3lzdGVtLm91dC5w cmludGxuKCJWYWx1ZSBmcm9tIHN0b3JlZCBmdW5jdGlvbiAiICsgdmFsKTsK CQljcy5jbG9zZSgpOwogICAgICAgICAKICAgICAgICAgICAgICAgIHJlcy5j bG9zZSgpOwogICAgICAgICAgICAgICAgc3RtdC5jbG9zZSgpOwogICAgICAg ICAgICAgICAgY29ubi5jbG9zZSgpOwogICAgICAgICAgICB9CiAgICAgICAg IAogICAgICAgICAgICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmcg YXJnc1tdKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICB0cnkgewog ICAgICAgICAgICAgICAgICAgIHNhbXBsZSB0ZXN0ID0gbmV3IHNhbXBsZSgp OwogICAgICAgICAgICAgICAgfSBjYXRjaChFeGNlcHRpb24gZXhjKSAKICAg ICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBTeXN0ZW0uZXJy LnByaW50bG4oIkV4Y2VwdGlvbiBjYXVnaHQuXG4iICsgZXhjKTsKICAgICAg ICAgICAgICAgICAgICBleGMucHJpbnRTdGFja1RyYWNlKCk7CiAgICAgICAg ICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICB9IAo= --0-595423857-1091161103=:92437--