469,352 Members | 2,204 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

How to escape all special characters in jsp??

Expand|Select|Wrap|Line Numbers
  1. String q = ("update institutes set institute_facilities='"+fac+"' where institute_id = '"+id+"' ");
How can escape all special characters & insert values to db???

Can anybody help me on this??


Thank You.
Dec 5 '08 #1
4 20127
JosAH
11,448 Expert 8TB
@dirtyhawk
What characters are 'special' to you?

kind regards,

Jos
Dec 5 '08 #2
chaarmann
785 Expert 512MB
There is a
java.net.UrlEncoder.encode()
function that you can use if you have problems with storing/retrieving special characters in the database.

Generally speaking, replacing character a by b in string q is done with:
q.replace(a, b).

If you have more complicated replacements, you should use a RE (Regular Expression) to do the job:
q.replaceAll(RE, b)

For Oracle database for example, you need to replace single quotation marks inside a string by two single quotation marks.
Dec 8 '08 #3
JosAH
11,448 Expert 8TB
Note that a PreparedStatement handles the necessary escaping of its parameterized arguments. Also note that URL escaping is something completely different.

kind regards,

Jos
Dec 8 '08 #4
chaarmann
785 Expert 512MB
@JosAH
Yes, I know.
It's just an expert-trick if you have problems with database specific issues like special characters or trimming:
- if your database can only store ascii-characters (or you don't know / have no rights to configure it to UTF-8), but you need to store some language-specific non-ascii-characters.
- if your database trims leading / trailing whitespaces (or appends some to fill for max-length) according to the column field type, but you want to preserve them. (space --> plus-sign)
- if you don't care how the data looks inside the database, but you care that it is 100% the same before encoding and after retrieving and decoding

You could also use xml encoding/decoding, but the issue with whitespace (cr/lf/tab/space, 0x00-0x1F) remains.
Dec 9 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Matthew Wieder | last post: by
6 posts views Thread by Walter L. Preuninger II | last post: by
6 posts views Thread by HK guy | last post: by
131 posts views Thread by Lawrence D'Oliveiro | last post: by
2 posts views Thread by simplicity | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.