jQuery in Action, 3rd edition
The moose likes JSP and the fly likes Problem in inserting special characters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Problem in inserting special characters" Watch "Problem in inserting special characters" New topic

Problem in inserting special characters

Mehak Daani

Joined: Oct 08, 2003
Posts: 16
I have a text box in which the description of a problem is written .... the textbox name is prdesc ;
now when prdesc holds the value = " dont or doesnot " ... this string doesn't give a problem but if my string contains special characters such as " Don't or Doesn't or "double quotes " " ....
it gives a problem while inserting in mysql database...!!!
it checked out in the database .. it inserts a "\" before every single and double quotes...... how would i handle this in jsp ..
my insert command at the time being is
insert into some table(problem)values ('"+prdesc+"');

please help...

Lisa D'Aniello
Ranch Hand

Joined: Sep 25, 2003
Posts: 42
Hi Mehak,
The single and double quotes have caused me a lot of frustration in the past. I don't know about the mysql database, but here is what I think is happening
You have this mystring = "don't"
then you have strSQL = "INSERT INTO myTable(myField) VALUES('mystring')"
it is seeing the ' in don't as the end of the string and then it doesn't know what to do with the t
I generally will use the replace method to replace ' with ` in the variable
the \ you are seeing means to take the next character literally, so any character following the \ should process (*I think*), but I do not know how it will affect your output.
hope this helps,
Lisa M.
Tim Baker
Ranch Hand

Joined: Oct 04, 2003
Posts: 541
that is one way, the other way and my preferred way is to use PreparedStatement which will replace special chars for you. look at the API for PreparedStatement to see how to use it.

Kim Jong II (North Korea's Dear Leader) said:Nuclear weapons don't kill people, people kill people.
Mathias Nilsson
Ranch Hand

Joined: Oct 13, 2003
Posts: 107
I agree with Tim Baker. The best approch is to use PreparedStatements.

Here you use setXXX to map the ? to a variable. When you use
ps.setString( 1 , UserName ); you map the UserName variable with the first ? character. There are more methods than setString of course. See
java.sql.PreparedStatement for more information

SCJP2 , MCP( 70-229 ) , Preparing For SCWDC
Lisa D'Aniello
Ranch Hand

Joined: Sep 25, 2003
Posts: 42
Thanks for posting this question Mehak - and please ingnore my primitive response! SHEESH!!! Why do I always insist on doing things the difficult way?!? :roll:
Mehak Daani

Joined: Oct 08, 2003
Posts: 16
hey guys ...
thanx for ue suggestion....... i was getting the same problem with the prepared statement ... but the newstr.replace() function really helped ... thanx .... a lot..!!
I agree. Here's the link: http://aspose.com/file-tools
subject: Problem in inserting special characters
It's not a secret anymore!