From: Chris Wareham <cwareham@londonandpartners.com>
To: JavaNoobie <vivek.mv@enzentech.com>
Cc: pgsql-jdbc@postgresql.org
Subject: Re: how to Escape single quotes with PreparedStatment
Date: Mon, 22 Aug 2011 11:35:19 +0100
Message-ID: <4E5230E7.60009@londonandpartners.com> (raw)
In-Reply-To: <1314008766751-4722898.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>
<4E5225B1.1030506@ahastie.net>
<1314007009734-4722805.post@n5.nabble.com>
<1314008766751-4722898.post@n5.nabble.com>
On 22/08/11 11:26, JavaNoobie wrote:
> Hi,
> The following code works properly.
> 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(?))OR (lower(?)='') LIMIT
> ? OFFSET ? "; // AND (lower(RESIDING_VILLAGE) LIKE (lower(?) OR
> (lower(?)='')) AND (lower(WENEXA_ID) LIKE (lower(?))OR(lower(?)=''))" ;
>
>
> log.info(query); // Get the wenexa ID to be passed from servlet
> here....
> stmt = con.prepareStatement(query);
> stmt.setString(1,"%"+name+"%");
> stmt.setString(2,"%"+name+"%");
> stmt.setInt(3,pageLimit);
> stmt.setInt(4,pageOffset);
>
> However , when I try the code -
> 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(?))OR (lower(?)='') AND
> (lower(RESIDING_VILLAGE) LIKE (lower(?) OR (lower(?)='')) AND
> (lower(WENEXA_ID) LIKE (lower(?))OR(lower(?)='')) LIMIT ? OFFSET ?";
> stmt = con.prepareStatement(query);
> stmt.setString(1,"%"+name+"%");
> stmt.setString(2,"%"+name+"%");
> stmt.setString(3,"%"+village+"%");
> stmt.setString(4,"%"+village+"%");
> stmt.setInt(5,pageLimit);
> stmt.setInt(6,pageOffset);
> rs= stmt.executeQuery();
>
> It throws the previous error:org.postgresql.util.PSQLException: ERROR:
> syntax error at or near "LIMIT"
> Position: 302
> at
> org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2102)
> at
> org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1835)
> Any idea why this could be happening?
>
Sigh. I really need more coffee at this time of the morning. The
setters need to having an incrementing index:
StringBuilder query = new StringBuilder("SELECT count(*) over() AS
rowcount, consumer_id, wenexa_id, consumer_name, contact_no,
residing_village FROM db_consumer WHERE TRUE");
if (!name.isEmpty()) {
query.append(" AND consumer_name ILIKE ?");
}
if (!village.isEmpty()) {
query.append(" AND residing_village ILIKE ?");
}
if (!wenexaid.isEmpty()) {
query.append(" AND wenexa_id ILIKE ?");
}
buf.append(" LIMIT ? OFFSET ?");
stmt = con.prepareStatement(query.toString());
int i = 0;
if (!name.isEmpty()) {
stmt.setString(++i, "%" + name + "%");
}
if (!village.isEmpty()) {
stmt.setString(++i, "%" + village + "%");
}
if (!wenexaid.isEmpty()) {
stmt.setString(++i, "%" + wenexaid + "%");
}
stmt.setInt(++i, pageLimit);
stmt.setInt(++i, pageOffset);
rs = stmt.executeQuery();
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: cwareham@londonandpartners.com, vivek.mv@enzentech.com
Subject: Re: how to Escape single quotes with PreparedStatment
In-Reply-To: <4E5230E7.60009@londonandpartners.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> <4E5225B1.1030506@ahastie.net> <1314007009734-4722805.post@n5.nabble.com> <1314008766751-4722898.post@n5.nabble.com> <4E5230E7.60009@londonandpartners.com>
* 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