Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

how to use LIKE operator with PreparedStatement

423200Jun 11 2004 — edited Jun 11 2004
Hi, I need to execute a query with the LIKE operator, but using a PreparedStatement. Can I do this, and if so what must my SQL look like with the wildcard characters '%' or '_'?

normal PS example: conn.prepareStatement("select * from mytable where name like ?");

If I try: conn.prepareStatement("select * from mytable where name like ?%");
I get: ORA-00911: invalid character

If I try: conn.prepareStatement("select * from mytable where name like '?%'");
I get: ORA-01006: bind variable does not exist

I must use a PreparedStatement, as my variable may contain illegal characters (like '), and using PreparedStatement.setString(1, var) will automatically escape it for me.

I could also use a normal Statement, but I need to escape my var ... is there a utility that will safely escape a String for an Oracle VARCHAR2??

Thanks in advance,
Stu Miller
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 9 2004
Added on Jun 11 2004
4 comments
2,032 views