• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem in inserting special characters

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 541
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..!!
 
Hey! You're stepping on my hand! Help me tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic