Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 4F52CB5DBEA for ; Mon, 22 Aug 2011 07:05:06 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 62746-03 for ; Mon, 22 Aug 2011 10:04:59 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from claranet-outbound-smtp03.uk.clara.net (claranet-outbound-smtp03.uk.clara.net [195.8.89.36]) by mail.postgresql.org (Postfix) with ESMTP id 29FA9B5DBDA for ; Mon, 22 Aug 2011 07:04:58 -0300 (ADT) Received: from [217.158.205.40] (port=50745 helo=BSP1.visitlondon.com) by relay03.mail.eu.clara.net (relay.clara.net [213.253.3.43]:1025) with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) id 1QvRNJ-0002oX-Ar (return-path ); Mon, 22 Aug 2011 10:04:57 +0000 Message-ID: <4E5229C8.1000300@londonandpartners.com> Date: Mon, 22 Aug 2011 11:04:56 +0100 From: Chris Wareham User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:6.0) Gecko/20110812 Thunderbird/6.0 MIME-Version: 1.0 To: JavaNoobie CC: Subject: Re: how to Escape single quotes with PreparedStatment References: <1313841345919-4718287.post@n5.nabble.com> In-Reply-To: <1313841345919-4718287.post@n5.nabble.com> Content-Type: text/plain; charset="ISO-8859-1"; format=flowed Content-Transfer-Encoding: 7bit Received-SPF: none 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, RCVD_IN_DNSWL_NONE=-0.0001 X-Spam-Level: X-Archive-Number: 201108/52 X-Sequence-Number: 22084 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();