Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to escape special characters used in SQL query?

Tags:

java

sql

escaping

Is there a Java library for escaping special characters from a string that is going to be inserted into an SQL query.

I keep writing code to escape various things, but I keep finding some new issue trips me up. So a library that takes care of all or most of the possibilities would be very handy.

EDIT: I am using MySQL (if that makes any difference).

like image 715
Ankur Avatar asked Jun 30 '11 05:06

Ankur


2 Answers

Well... jdbc. Pass the strings as parameters, and don't append them to the query string

like image 174
Maurice Perry Avatar answered Nov 01 '22 14:11

Maurice Perry


A little bit more research points me to this:

http://devwar.blogspot.com/2010/06/how-to-escape-special-characters-in.html

Which suggests to use apache.commons.lang.StringEscapeUtils, I will try this out

like image 38
Ankur Avatar answered Nov 01 '22 14:11

Ankur