Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 40713B5DBDC for ; Sun, 21 Aug 2011 07:54:04 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 20994-04 for ; Sun, 21 Aug 2011 10:53:57 +0000 (UTC) X-Greylist: delayed 00:08:44.771019 by SQLgrey-1.7.6 Received: from mail.softperience.eu (static-ip-217-118-26-141.inaddr.ip-pool.com [217.118.26.141]) by mail.postgresql.org (Postfix) with ESMTP id 7038DB5DBD0 for ; Sun, 21 Aug 2011 07:53:57 -0300 (ADT) Received: from localhost.localnet (ceb201.neoplus.adsl.tpnet.pl [83.30.177.201]) by mail.softperience.eu (Postfix) with ESMTPA id D4E40C00078; Sun, 21 Aug 2011 12:44:51 +0200 (CEST) From: =?utf-8?q?Rados=C5=82aw_Smogura?= To: pgsql-jdbc@postgresql.org Subject: Re: how to Escape single quotes with PreparedStatment Date: Sun, 21 Aug 2011 12:45:14 +0200 User-Agent: KMail/1.13.7 (Linux/2.6.39-gentoo-r3; KDE/4.6.5; x86_64; ; ) Cc: JavaNoobie References: <1313841345919-4718287.post@n5.nabble.com> In-Reply-To: <1313841345919-4718287.post@n5.nabble.com> MIME-Version: 1.0 Content-Type: Text/Plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <201108211245.14465.mail@smogura.eu> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-10 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201108/46 X-Sequence-Number: 22078 JavaNoobie Saturday 20 of August 2011 13:55:45 > Hi All, > I'm trying to write a preparedstatement query as below. >=20 > String query=3D "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 + "')=3D'')) AND (lower(RESIDING_VILLAGE) LIKE > (lower('%"+village+"%')) OR (lower('" + village + "')=3D'')) AND > (lower(WENEXA_ID) LIKE (lower('%"+wenexaid+"%'))OR(lower( '" + wenexaid + > "')=3D'')) LIMIT '"+pageLimit+"'OFFSET '"+pageOffset+"'"; >=20 > stmt =3D con.prepareStatement(query); > rs=3D stmt.executeQuery(); >=20 > 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. Prepared statements do escaping (actualy PS do not make this, because those= =20 sends just raw parameters). In order to make this your statement should loo= k=20 like (e.g.) (lower('" + name + "') =3D> (lower(?) then you call ps.setString(1, /*index of name*/, name); Driver can't know what should be escaped or should not - in your query you= =20 just pass full query string. Consider following (SQL hacking guide) code String query =3D "SELECT * FROM dummy WHERE name =3D '" + something + "'"; Driver should have possibility to look in your constructing expression to t= ry=20 to guess that name is parameter (C#, allows simillar constructs), but it ca= n't=20 because Java will do StringBuilder sb =3D new StringBuillder(); sb.append("SELECT * FROM dummy WHERE name =3D '"); sb.append(something); sb.append("'"); query =3D sb.toString(); =46rom other side, one may want that "something" will be longer (something = =3D=20 "'SomeName' and surname =3D 'SomeSureName"); And some one may want: something =3D "'SomeName'; DROP TABLE security_log; SELECT * FROM dummy whe= re=20 name=3D'd" Regards, Radek