Skip site navigation (1) Skip section navigation (2)

Re: CallableStatement and getUpdateCount

From: "Albe Laurenz" <laurenz(dot)albe(at)wien(dot)gv(dot)at>
To: "Sam Lawrence *EXTERN*" <sam(at)fsbtech(dot)com>,<pgsql-jdbc(at)postgresql(dot)org>
Subject: Re: CallableStatement and getUpdateCount
Date: 2008-04-01 08:03:34
Message-ID: D960CB61B694CF459DCFB4B0128514C201ED261B@exadv11.host.magwien.gv.at (view raw or flat)
Thread:
Lists: pgsql-jdbc
Sam Lawrence wrote:
> I have a query about using CallableStatement with functions that return 
> a SETOF. I know the "Calling Stored Functions" documentation says use a 
> Statement or a PreparedStatement - I'm in the process of porting over a 
> database and would like to leave my calling code the same - that means 
> CallableStatement (more in a moment).
> 
> First, an example. A simple function (I know the SETOF is redundant in 
> this example, normally it wouldn't be) -
> 
>     CREATE OR REPLACE FUNCTION cstest()
>     RETURNS SETOF integer AS
>     $$
>         SELECT 1;
>     $$
>     LANGUAGE 'sql' VOLATILE;
> 
> The calling code -
> 
>     Class.forName("org.postgresql.Driver");
>     Connection con = DriverManager.getConnection("...");
>    
>     CallableStatement cs = con.prepareCall("{call cstest()}");
>     cs.execute();
>    
>     System.out.println("update count - should be -1? " + cs.getUpdateCount());
>    
>     ResultSet rs = cs.getResultSet();
>     while(rs.next())
>     {
>         System.out.println(rs.getInt(1));
>     }
>     rs.close();
>     cs.close();
>     con.close();
> 
> The code works fine and returns the record correctly. My problem is the 
> getUpdateCount after the execute - from the java.sql.Statement 
> documentation "if the result is a ResultSet object or there are no more 
> results, -1 is returned". It actually comes back as 1. Is this a bug or 
> have I missed something?
> 
> I'm running PostgreSQL 8.3.0 with postgresql-8.3-603.jdbc4.

Hmmm. getUpdateCount() is defined in
org/postgresql/jdbc2/AbstractJdbc2Statement.java as

    public int getUpdateCount() throws SQLException
    {
[...]
        if (isFunction)
            return 1;
[...]
    }

So it will always return 1 for a callable statement.

There is no comment in the source. Does anybody know why that is?

Yours,
Laurenz Albe

In response to

Responses

pgsql-jdbc by date

Next:From: Shavonne Marietta WijesingheDate: 2008-04-01 09:19:36
Subject: JSP to PostgreSql
Previous:From: Kris JurkaDate: 2008-04-01 07:22:24
Subject: Re: Deadlock while using getNotifications() and Statement.executeQuery()

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group