File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes PreparedStatement using LIKE Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "PreparedStatement using LIKE" Watch "PreparedStatement using LIKE" New topic

PreparedStatement using LIKE

Javoso Torso
Ranch Hand

Joined: Aug 15, 2003
Posts: 88

I�m tired of searching the internet for this one

using a PreparedStatement with the query
I also tried other similar ways like:
ps.setString(2, "%" + str2);
ps.setString(2, '%' + str2);
I am not having the right result. I tested with a DB-Tool + I am sure it is not working.

Jamie Robertson wrote in Jamie Comment:

keep in mind that every driver/database combo deals with special characters differently. Some escape all special characters, some escape only single quotes. So you'd have to find out from your jdbc driver documentation how your specific driver handles the special character %. If it escapes it, your search is looking for the % literally. If it doesn't escape it, then it will work as the intended wildcard. The same code used in the example you've linked to works fine as a wildcard search using Oracle drivers to an Oracle 8.1.7 database.

I think that is my case: the code is taking % literally and not using it as a wildCard

the driver is sun.jdbc.odbc.JdbcOdbcDriver
SDK 1.4.1_01
database = sql server

anyone knows how can I deal with it...

thanks + regards,
[ August 03, 2004: Message edited by: Javoso Torso ]

I hate 'trial and error'<br />and I am doing it all the time
Javoso Torso
Ranch Hand

Joined: Aug 15, 2003
Posts: 88
got something work!:

there are rumors that some mssql drivers have a bug that makes setString() not work with "LIKE"

So, that's my contribution for all the people looking for hours for a solution...

[ August 03, 2004: Message edited by: Javoso Torso ]
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

Thanks for posting the follow-up!
I agree. Here's the link:
subject: PreparedStatement using LIKE
It's not a secret anymore!