Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem in inserting special characters

 
Mehak Daani
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi..
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...
thanx
 
Lisa D'Aniello
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 541
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mathias Nilsson
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Lisa D'Aniello
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic