From: Andrew Hastie <andrew@ahastie.net>
To: pgsql-jdbc@postgresql.org
Subject: Re: how to Escape single quotes with PreparedStatment
Date: Mon, 22 Aug 2011 10:47:29 +0100
Message-ID: <4E5225B1.1030506@ahastie.net> (raw)
In-Reply-To: <1314000874022-4722528.post@n5.nabble.com>
References: <1313841345919-4718287.post@n5.nabble.com>
<201108211245.14465.mail@smogura.eu>
<1313988267050-4722152.post@n5.nabble.com>
<201108220946.37783.ml-tb@emagixx.de>
<1314000874022-4722528.post@n5.nabble.com>
I think you're defining the "?" parameter incorrectly for the PreparedStatement.
Try this instead:-
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(?))";
query.setString(1,"%Fred%");
Where "Fred" is the string you're searching for.
You cannot use the ? token to substitute part of data value, only a whole value. As you are parameterizing the "pattern" argument to the LIKE expression which is a String argument, you must pass the complete pattern as a String parameter.
Hope this helps,
Andrew
On 22/08/11 09:14, JavaNoobie wrote:
> Hi,
> I tried that example but it threw an error as follows.
> org.postgresql.util.PSQLException: ERROR: syntax error at or near "%"
> Position: 158
> at
> org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2102)
> at
> org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1835)
>
> Why could this be happening?
>
> --
> View this message in context: http://postgresql.1045698.n5.nabble.com/how-to-Escape-single-quotes-with-PreparedStatment-tp4718287p...
> Sent from the PostgreSQL - jdbc mailing list archive at Nabble.com.
>
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: <4E5225B1.1030506@ahastie.net>
References: <1313841345919-4718287.post@n5.nabble.com> <201108211245.14465.mail@smogura.eu> <1313988267050-4722152.post@n5.nabble.com> <201108220946.37783.ml-tb@emagixx.de> <1314000874022-4722528.post@n5.nabble.com> <4E5225B1.1030506@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