Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

special characters

Status
Not open for further replies.

sandeepmur

Programmer
Dec 14, 2003
295
PT
Hi,

I am inserting text into a MySQL database using a JSP. my problem is mostly with the ' character in the text which results in an error.

I havent had any problem so far inserting other kinds of special characters, accents such as ",~´`

Can anyone please suggest the best way to treat special characters in a JSP in particular the ' character.

Thnx in adv,
sg
 
You propable constructing the insert sql statement manually. The problem is ' character breaking the statement syntax. If you use java.sql.PreparedStatement, and use setString() to set the value, JDBC driver will automatically fix the ' character or any other special character for you.

Best way is to use PreparedStatement. Otherwise you will have to manually replace "'" (single quote) by "''" (2 single quotes) in the string value.
 
the safest way is to use unicode, i.e "\u0000". hence, if
the unicode char is 0022(better check it yourself), you can just concatenate it with the rest of the string:-

mySql = "SELECT * FROM info WHERE name = \u0000" + nameInput + "\u0000";

of course, for optimization, use StringBuffer instead if String object;)

~za~
You can't bring back a dead thread!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top