Received: from localhost (maia-1.hub.org [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id E6DA09FB603 for ; Thu, 25 Jan 2007 19:34:39 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-new, port 10024) with ESMTP id 49173-07 for ; Thu, 25 Jan 2007 19:34:36 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from floppy.pyrenet.fr (floppy.pyrenet.fr [194.116.145.2]) by postgresql.org (Postfix) with ESMTP id 204399FB675 for ; Thu, 25 Jan 2007 19:34:36 -0400 (AST) Received: by floppy.pyrenet.fr (Postfix, from userid 106) id BBAE3308F7; Fri, 26 Jan 2007 00:34:34 +0100 (MET) From: Tobias Thierer X-Newsgroups: pgsql.interfaces.jdbc Subject: escape string for pgsql (using jdbc/java)? Date: Fri, 26 Jan 2007 12:34:31 +1300 Organization: Hub.Org Networking Services Lines: 37 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@news.hub.org User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.9) Gecko/20061211 SeaMonkey/1.0.7 To: pgsql-jdbc@postgresql.org X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.001 tagged_above=0 required=5 tests=BAYES_50 X-Spam-Level: X-Archive-Number: 200701/161 X-Sequence-Number: 16526 Hi, I'm writing a servlet that gets a few strings and puts them into a pgsql database. In assembling an insert statement such as INSERT INTO table column1='value1' column2='value2' etc., of course I have to make sure an attacker can't put things into value1 that will breaky my system (such as something that contains a ' which will then be interpreted as terminating the string). In other words, I have to escape value* so that it's safe to use in an sql statement (more specifically inside a string). I was previously using MySQL and escaped strings following the document at: http://dev.mysql.com/doc/refman/5.0/en/string-syntax.html But I couldn't find a corresponding specification for pgsql. The only way of doing this through JDBC that I'm aware of is to prepare a statement first, which just seems wrong because my insert statement is generated dynamically and executed exactly once (the subset of the columns for which a value is actually set change every time the code is run). So, 1.) Is there a built-in method somewhere in the jdbc driver that escapes strings and makes them safe to use in an SQL statement (inside a string)? 2.) Which characters do I need to escape for pgsql? Is ' the only one, and I need to escape it as '' ? Do I need to escape \ ? Will I need to escape all the characters that I escaped for MySQL? Where can I find out more? Cheers, Tobias