Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 10639B5DBDC for ; Mon, 22 Aug 2011 09:12:39 -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 01277-01-9 for ; Mon, 22 Aug 2011 12:12:32 +0000 (UTC) X-Greylist: from auto-whitelisted 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 2483DB5DD01 for ; Mon, 22 Aug 2011 09:12:08 -0300 (ADT) Received: from mail.softperience.eu (static-ip-217-118-26-141.inaddr.ip-pool.com [217.118.26.141]) by mail.softperience.eu (Postfix) with ESMTPA id 9F2E0C000AF; Mon, 22 Aug 2011 14:11:53 +0200 (CEST) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Date: Mon, 22 Aug 2011 14:11:53 +0200 From: =?UTF-8?Q?Rados=C5=82aw_Smogura?= To: Chris Wareham Cc: JavaNoobie , Subject: Re: how to Escape single quotes with PreparedStatment In-Reply-To: <4E5229C8.1000300@londonandpartners.com> References: <1313841345919-4718287.post@n5.nabble.com> <4E5229C8.1000300@londonandpartners.com> Message-ID: X-Sender: rsmogura@softperience.eu User-Agent: Roundcube Webmail/0.5.1 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/58 X-Sequence-Number: 22090 On Mon, 22 Aug 2011 11:04:56 +0100, Chris Wareham wrote: > On 20/08/11 12:55, 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. >> > > You should use placeholders (the ? character) in your prepared > statement, and then call the setter methods to insert your query > terms. > The JDBC driver will take care of all escaping and quoting for you. > You > should also consider converting the query columns to full text > indexes > using tsearch. And finally, you can also simplify your query by only > searching for the non-empty terms. > > 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); > > if (!name.isEmpty()) { > stmt.setString(2, "%" + name + "%"); > } > > if (!village.isEmpty()) { > stmt.setString(4, "%" + village + "%"); > } > > if (!wenexaid.isEmpty()) { > stmt.setString(6, "%" + wenexaid + "%"); > } > > stmt.setInt(7, pageLimit); > stmt.setInt(8, pageOffset); > > rs = stmt.executeQuery(); Just for info, if name is empty, then probably there will be no parameter 8, etc. Use following block: int i=2; if (!name.isEmpty()) { stmt.setString(i, "%" + name + "%"); i++; } etc... stmt.setInt(i, pageOffset); Regards