Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 6B26C632738 for ; Sun, 12 Jul 2009 05:13:12 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 83313-09 for ; Sun, 12 Jul 2009 05:13:01 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from smtp.opticon.hu (smtp.opticon.hu [195.56.234.123]) by mail.postgresql.org (Postfix) with SMTP id 6DFA4632453 for ; Sun, 12 Jul 2009 05:13:00 -0300 (ADT) Received: (qmail 14417 invoked from network); 12 Jul 2009 08:12:58 -0000 Received: from unknown (HELO ?192.168.1.13?) (85.66.103.79) by smtp.opticon.hu with SMTP; 12 Jul 2009 08:12:48 -0000 Message-ID: <4A599B14.4090002@hdsnet.hu> Date: Sun, 12 Jul 2009 10:13:08 +0200 From: =?UTF-8?B?U3phYsOzIFRhbcOhcw==?= User-Agent: Thunderbird 2.0.0.22 (Windows/20090605) MIME-Version: 1.0 To: pgsql-jdbc@postgresql.org Subject: Re: calling a stored function which return set of records References: <4A5761B8.7030401@hdsnet.hu> <24445698.post@talk.nabble.com> In-Reply-To: <24445698.post@talk.nabble.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200907/18 X-Sequence-Number: 19786 Now I modified my java code : c.setAutoCommit(false); // Procedure call. CallableStatement cs = (CallableStatement) c.prepareCall("{ call ? = get_recipe_data[(?,?,?,?,?,?,?)] }"); cs.setInt(1, 1); cs.registerOutParameter(2, Types.INTEGER); cs.registerOutParameter(3, Types.FLOAT); cs.registerOutParameter(4, Types.FLOAT); cs.registerOutParameter(5, Types.FLOAT); cs.registerOutParameter(6, Types.FLOAT); cs.registerOutParameter(7, Types.FLOAT); cs.execute(); cs.close(); Now I get an error message that parameter 8 is not specified, which I understand beacuse there are 8 placeholders ( "?" ) , but I can't find out what should i define for the type of SET OF RECORDS as the functions returns this type of data. However the stored procedure called from postgresql client program works well. > You are creating the CallableStatement wrong. > c.prepareCall("{ call get_recipe_kcal( ?,?,?,?,?,? ) }") > > Read more here: > http://java.sun.com/javase/6/docs/technotes/guides/jdbc/getstart/callablestatement.html#1003293 > > I suggest reading the whole thing here: > http://java.sun.com/javase/6/docs/technotes/guides/jdbc/ > > > Szabó Tamás wrote: > >> Hello! >> >> I have a stored function in a postgresql databse, and I want to call it >> from my java program. >> >> The stored function is like this: >> >> CREATE OR REPLACE FUNCTION get_recipe_data(recipe_id integer, >> OUT recipe_id integer, >> OUT kcal real, >> OUT kj real, >> OUT protein real, >> OUT fat real, >> OUT carbohydrates real >> ) RETURNS SETOF record >> >> AS $$ >> >> BEGIN >> >> RETURN QUERY >> SELECT recipes_ingredients_conn.recipe_id, >> sum(recipes_ingredients_conn.amount / ingredients.amount * >> ingredients.kcal), >> sum(recipes_ingredients_conn.amount / ingredients.amount * >> ingredients.kj), >> sum(recipes_ingredients_conn.amount / ingredients.amount * >> ingredients.protein), >> sum(recipes_ingredients_conn.amount / ingredients.amount * >> ingredients.fat), >> sum(recipes_ingredients_conn.amount / ingredients.amount * >> ingredients.carbohydrates) >> >> FROM recipes_ingredients_conn, ingredients >> WHERE (recipes_ingredients_conn.ingredient_id = >> ingredients.ingredient_id) AND (recipes_ingredients_conn.recipe_id = >> recipe_id) >> GROUP BY recipes_ingredients_conn.recipe_id; >> END >> >> $$ >> >> LANGUAGE plpgsql; >> >> >> The code sheet from my java program is like: >> >> ... >> // Turn transactions off. >> c.setAutoCommit(false); >> // Procedure call, i don't know if this is the right way to >> define the stored function >> // Maybe do i use a refcursor or something like this??? >> CallableStatement upperProc = c.prepareCall("{ (?,?,?,?,?,?) >> = call get_recipe_kcal( ? ) }"); >> >> upperProc.registerOutParameter(1, Types.INTEGER); >> upperProc.registerOutParameter(2, Types.REAL); >> upperProc.registerOutParameter(3, Types.REAL); >> upperProc.registerOutParameter(4, Types.REAL); >> upperProc.registerOutParameter(5, Types.REAL); >> upperProc.registerOutParameter(6, Types.REAL); >> >> upperProc.setInt(7, 1); >> >> upperProc.execute(); >> double i = upperProc.getDouble(3); >> >> System.out.println(i); >> upperProc.close(); >> ... >> >> When I try to run the java program I get the following error message: >> Error: Malformed function or procedure escape syntax at offset 2. >> I don't really know what I'm doing wrong, i read through some articles >> about the problem, but I couldn't find a solution. >> Please help me If you can. Thnaks! >> >> >> -- >> Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org) >> To make changes to your subscription: >> http://www.postgresql.org/mailpref/pgsql-jdbc >> >> >> > >