From: Andrew Hastie <andrew@ahastie.net>
To: pgsql-jdbc@postgresql.org
Subject: Re: how to Escape single quotes with PreparedStatment
Date: Sat, 20 Aug 2011 15:23:53 +0100
Message-ID: <4E4FC379.3050709@ahastie.net> (raw)
In-Reply-To: <4E4FCE49.2000605@ttc-cmc.net>
References: <1313841345919-4718287.post@n5.nabble.com>
<4E4FCE49.2000605@ttc-cmc.net>
Better still, with the Postgres driver once you have prepared the
statement (including when you have "?" parameters for substitution), you
can use the "toString()" method of the PreparedStatement object to see
what the final SQL statement is that will be executed.
So, try this to get a better trace of what is failing:-
PreparedStatement stmt = con.prepareStatement(query);
System.out.println("SQL=" + stmt.toString());
ResultSet rs= stmt.executeQuery();
Hope this helps.
Andrew
On 20/08/2011 16:10, dmp wrote:
> JavaNoobie wrote:
> > Hi All,
> > I'm trying to write a preparedstatement query as below.
> >
> > String query= "SELECT count(*) over () as ROWCOUNT,
> > CONSUMER_ID,WENEXA_ID,CONSUMER_NAME,CONTACT_NO,residing_village from
> > db_consumer WHERE (lower(CONSUMER_NAME) LIKE (lower('%"+name+"%'))OR
> > (lower('" + name + "')='')) AND (lower(RESIDING_VILLAGE) LIKE
> > (lower('%"+village+"%')) OR (lower('" + village + "')='')) AND
> > (lower(WENEXA_ID) LIKE (lower('%"+wenexaid+"%'))OR(lower( '" +
> wenexaid +
> > "')='')) LIMIT '"+pageLimit+"'OFFSET '"+pageOffset+"'";
> >
> > stmt = con.prepareStatement(query);
> > rs= stmt.executeQuery();
> >
> > However , the query fails with postgresql when a double quote is
> passed into
> > it.I was under the impression that Prepared statement would take
> care of the
> > same . But can anyone explain why I'm getting the error?
> > Thank you.
>
> Perhaps to get a better idea of what exactly the query string
> is that is being executed you could:
>
> System.out.println(query);
> stmt = con.prepareStatement(query);
> rs= stmt.executeQuery();
>
> danap.
>
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-jdbc@postgresql.org
Cc: andrew@ahastie.net
Subject: Re: how to Escape single quotes with PreparedStatment
In-Reply-To: <4E4FC379.3050709@ahastie.net>
References: <1313841345919-4718287.post@n5.nabble.com> <4E4FCE49.2000605@ttc-cmc.net> <4E4FC379.3050709@ahastie.net>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox