wood burning stoves 2.0*
The moose likes JDBC and the fly likes inserting text with single and doubl quotes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "inserting text with single and doubl quotes" Watch "inserting text with single and doubl quotes" New topic
Author

inserting text with single and doubl quotes

Gul Khan
Ranch Hand

Joined: Sep 03, 2003
Posts: 173
Hi
When i try inserting a text with quotes single or double. the Db generates error on the sql statement. Can someone plz help me how to implement it so it can insert the quotes in text and not show errors.
Thanks
Gul
Bhupendra Malviya
Greenhorn

Joined: Oct 01, 2003
Posts: 15
Hi Gul,
Are you using "preparedStatement" to insert values.
If not then give it a shot.I didnt get any error using Prepared Statement on Ms Access.
Gul Khan
Ranch Hand

Joined: Sep 03, 2003
Posts: 173
Thanks Bhupendra
I am not using prepared statement. and to move to that i would have to change alot of modules. Is there any way i can do it in normal Statement.
I am using MySQL by the way.
Bhupendra Malviya
Greenhorn

Joined: Oct 01, 2003
Posts: 15
Hi Gul,
The only other method that I can think of is ,
You will have to parse your text in search of single quote(') and insert a escape character for java and your database language.
eg: the following peice of code works for Access/Oracle database(Sorry i am not conversant with Mysql)
query.executeUpdate("Insert into users values('Bhupi\''s')");
where \(back slash) is the escape character for java
'(Single Quote) is the escape charcter for Oracle Sql
and second '(Single Quote) is the quote in the actual text
Hope it solves ur problem let me know at this forum or mebhupendra@yahoo.co.in
Gul Khan
Ranch Hand

Joined: Sep 03, 2003
Posts: 173
Thanks
the single quote escape character also works with MySQL. Now let's c which option is better.. change all statements to prepared statements at more than a 100 places or search for quotes in every form input at about the same number of places. I think i should go with the prepared statement, more beneficial on long run.
Thanks again for the help
 
wood burning stoves
 
subject: inserting text with single and doubl quotes
 
Similar Threads
escape punctuation in string
A stange error while storing single quote in a table
How to handle comma in the text field?
Failed to insert text with quotes
Basic question - is version='1.0' valid