tags:

views:

51

answers:

1

I noticed that when I use a PreparedStatement it doesn't seem to escape certain wild-card characters like '%' or '_'. I know these can be escaped in MySql using a backslash. This made me wonder, what characters will a PreparedStatement escape?

+2  A: 

PreparedStatement doesn't escape anything - it relies on database support for precompiled statements.

That is, PreparedStatement never substitutes ?s for parameter values in order to form a literal query string. Instead, it sends a query string with placeholders to the database and uses database support to bind query parameters (however, it may depend on JDBC driver implementation).

axtavt
Interesting, what does that support look like for MySql?
stevebot
MySql supports prepared statements in the same way on the server side. How it looks in your code depends on the driver you are using.
Alan Geleynse
I believe many drivers actually do insert escaped argument text into the SQL text.
Tom Hawtin - tackline
@stevebot: Quick googling showed this: http://dev.mysql.com/tech-resources/articles/4.1/prepared-statements.html
axtavt
@Tom: I don't think it's the case for major databases.
axtavt