From: Kevin Grittner <kgrittn@ymail.com>
To: David Johnston <polobo@yahoo.com>
To: pgsql-jdbc@postgresql.org <pgsql-jdbc@postgresql.org>
Subject: Re: single quotation confusion
Date: Sun, 7 Apr 2013 07:11:22 -0700 (PDT)
Message-ID: <1365343882.92008.YahooMailNeo@web162902.mail.bf1.yahoo.com> (raw)
In-Reply-To: <1365297044098-5751093.post@n5.nabble.com>
References: <1365261492317-5751065.post@n5.nabble.com>
<1365297044098-5751093.post@n5.nabble.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-jdbc>
David Johnston <polobo@yahoo.com> wrote:
> R10 wrote
>> for example if a String txt = "it's cold" and to be inserted
>> into a sql query "insert into test values ('"+txt+"')";
>> it gives me
>> syntax error at or near "s"
>>
>> any ideas how to solve that problem ??
>
> In short you do not build queries by directly concatenating
> user-supplied data. You use a PREPARED STATEMENT with
> placeholders and then use the setXXX methods to map the data.
>
> SQL: INSERT INTO test VALUES (?)
Yeah, David's suggestion is the way to go. Trying to handle this
in an ad hoc fashion opens you to a class of attack known as "SQL
injection". When concatenating without proper handling of quotes
(which drivers will do for you) you should consider yourself
*lucky* if the worst you have seen is errors.
http://xkcd.com/327/
The above link also illustrates why you shouldn't run the
application under a user ID which has permission to execute DML.
--
Kevin Grittner
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company
--
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbc
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-jdbc@postgresql.org
Cc: kgrittn@ymail.com, polobo@yahoo.com
Subject: Re: single quotation confusion
In-Reply-To: <1365343882.92008.YahooMailNeo@web162902.mail.bf1.yahoo.com>
References: <1365261492317-5751065.post@n5.nabble.com> <1365297044098-5751093.post@n5.nabble.com> <1365343882.92008.YahooMailNeo@web162902.mail.bf1.yahoo.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox