pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
single quotation confusion
3+ messages / 3 participants
[nested] [flat]

* single quotation confusion
@ 2013-04-06 15:18 R10 <ymyib13@hotmail.com>
  2013-04-07 01:10 ` Re: single quotation confusion David Johnston <polobo@yahoo.com>
  0 siblings, 1 reply; 3+ messages in thread

From: R10 @ 2013-04-06 15:18 UTC (permalink / raw)
  To: pgsql-jdbc

i am connecting a postgresql database to a netbeans form , and when i try to
execute a query which includes a string entered through a Jtextfield that
contain the special charachter " ' " it reads it as a single quotation
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 ??





--
View this message in context: http://postgresql.1045698.n5.nabble.com/single-quotation-confusion-tp5751065.html
Sent from the PostgreSQL - jdbc mailing list archive at Nabble.com.


-- 
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbc



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: single quotation confusion
  2013-04-06 15:18 single quotation confusion R10 <ymyib13@hotmail.com>
@ 2013-04-07 01:10 ` David Johnston <polobo@yahoo.com>
  2013-04-07 14:11   ` Re: single quotation confusion Kevin Grittner <kgrittn@ymail.com>
  0 siblings, 1 reply; 3+ messages in thread

From: David Johnston @ 2013-04-07 01:10 UTC (permalink / raw)
  To: pgsql-jdbc

R10 wrote
> i am connecting a postgresql database to a netbeans form , and when i try
> to execute a query which includes a string entered through a Jtextfield
> that contain the special charachter " ' " it reads it as a single
> quotation
> 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 (?)

Others may be able to provide more concrete suggestions but otherwise find
some books/articles on database programming in Java.  This is not a
PostgreSQL specific topic/solution.  I've known this for so long now I
really cannot point you to any educational materials.

David J.




--
View this message in context: http://postgresql.1045698.n5.nabble.com/single-quotation-confusion-tp5751065p5751093.html
Sent from the PostgreSQL - jdbc mailing list archive at Nabble.com.


-- 
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbc



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: single quotation confusion
  2013-04-06 15:18 single quotation confusion R10 <ymyib13@hotmail.com>
  2013-04-07 01:10 ` Re: single quotation confusion David Johnston <polobo@yahoo.com>
@ 2013-04-07 14:11   ` Kevin Grittner <kgrittn@ymail.com>
  0 siblings, 0 replies; 3+ messages in thread

From: Kevin Grittner @ 2013-04-07 14:11 UTC (permalink / raw)
  To: David Johnston <polobo@yahoo.com>; pgsql-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



^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2013-04-07 14:11 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2013-04-06 15:18 single quotation confusion R10 <ymyib13@hotmail.com>
2013-04-07 01:10 ` David Johnston <polobo@yahoo.com>
2013-04-07 14:11   ` Kevin Grittner <kgrittn@ymail.com>

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