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

Re: Does PG's JDBC support prepared statements at all?

From: "Thomas O'Dowd" <tom(at)nooper(dot)com>
To: "Dr(dot) Evil" <drevil(at)sidereal(dot)kz>
Cc: pgsql-jdbc(at)postgresql(dot)org
Subject: Re: Does PG's JDBC support prepared statements at all?
Date: 2001-10-21 02:37:29
Message-ID: 20011021113729.V7183@beast.uwillsee.com (view raw or flat)
Thread:
Lists: pgsql-jdbcpgsql-patches
On Sat, Oct 20, 2001 at 11:14:22PM -0000, Dr. Evil wrote:
> 
> There is a bunch of documentation for prepared statements in PG's
> JDBC, it seems that the only thing prepared statements do is throw
> exceptions.
> 
> Here's some code I'm trying:
> 
>                 String newvalue = "This is a new value";
>                 int accountnumber = 54;
>                 String qstring = "UPDATE foo SET message = '?' WHERE number = ?";
> 		PreparedStatement st = db.prepareStatement(qstring);
>                 st.setString(1, newvalue);
>                 st.setInt(2, accountnumber);
>                 st.execute();
>                 st.clearParameters();
>                 st.close();
> 
> and I always get a Parameter index out of range error, which seems
> impossible.  Any idea what's going on?

You shouldn't quote the ? for the string. By calling the setString()
method, it will add the quotes for you. So I guess what is happening is
that the preparedstatement parser ignores quoted question marks and just
finds 1 variable, when you call setstring on 1 it sets the number= part,
and then when you call setInt(2) you are getting the index out of range.
The proper qstring should be:

"UPDATE foo SET message = ? WHERE number = ?"

Cheers,

Tom.
-- 
Thomas O'Dowd. - Nooping - http://nooper.com
tom(at)nooper(dot)com - Testing - http://nooper.co.jp/labs

In response to

Responses

pgsql-patches by date

Next:From: Gunnar RønningDate: 2001-10-21 03:24:33
Subject: Re: [PATCHES] Ant configuration
Previous:From: Dr. EvilDate: 2001-10-20 23:14:22
Subject: Does PG's JDBC support prepared statements at all?

pgsql-jdbc by date

Next:From: Gunnar RønningDate: 2001-10-21 03:24:33
Subject: Re: [PATCHES] Ant configuration
Previous:From: Dr. EvilDate: 2001-10-20 23:14:22
Subject: Does PG's JDBC support prepared statements at all?

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