Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 0131EB5DBBF for ; Sat, 20 Aug 2011 12:24: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 80928-10 for ; Sat, 20 Aug 2011 15:23:57 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from cpanelsmarthost1.zen.co.uk (cpanelsmarthost1.zen.co.uk [82.71.204.225]) by mail.postgresql.org (Postfix) with ESMTP id B40D4B5D803 for ; Sat, 20 Aug 2011 12:23:57 -0300 (ADT) Received: from [88.98.24.71] (helo=shcp03.hosting.zen.net.uk) by cpanelsmarthost1.zen.co.uk with esmtp (Exim 4.69) (envelope-from ) id 1QunOt-0008Cq-Ja for pgsql-jdbc@postgresql.org; Sat, 20 Aug 2011 15:23:55 +0000 Received: from host217-43-133-144.range217-43.btcentralplus.com ([217.43.133.144] helo=[192.168.0.3]) by shcp03.hosting.zen.net.uk with esmtpsa (TLSv1:AES256-SHA:256) (Exim 4.69) (envelope-from ) id 1QunOo-00041C-Dh for pgsql-jdbc@postgresql.org; Sat, 20 Aug 2011 16:23:50 +0100 Message-ID: <4E4FC379.3050709@ahastie.net> Date: Sat, 20 Aug 2011 15:23:53 +0100 From: Andrew Hastie User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20110624 Thunderbird/5.0 MIME-Version: 1.0 To: pgsql-jdbc@postgresql.org Subject: Re: how to Escape single quotes with PreparedStatment References: <1313841345919-4718287.post@n5.nabble.com> <4E4FCE49.2000605@ttc-cmc.net> In-Reply-To: <4E4FCE49.2000605@ttc-cmc.net> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - shcp03.hosting.zen.net.uk X-AntiAbuse: Original Domain - postgresql.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - ahastie.net 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/44 X-Sequence-Number: 22076 Better still, with the Postgres driver once you have prepared the statement (including when you have "?" parameters for substitution), you can use the "toString()" method of the PreparedStatement object to see what the final SQL statement is that will be executed. So, try this to get a better trace of what is failing:- PreparedStatement stmt = con.prepareStatement(query); System.out.println("SQL=" + stmt.toString()); ResultSet rs= stmt.executeQuery(); Hope this helps. Andrew On 20/08/2011 16:10, dmp wrote: > 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. > > Perhaps to get a better idea of what exactly the query string > is that is being executed you could: > > System.out.println(query); > stmt = con.prepareStatement(query); > rs= stmt.executeQuery(); > > danap. >