aspose file tools
The moose likes JDBC and the fly likes Preparedstatement with like where clause and the field contain %, how to escape? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Reply Bookmark "Preparedstatement with like where clause and the field contain %, how to escape?" Watch "Preparedstatement with like where clause and the field contain %, how to escape?" New topic
Author

Preparedstatement with like where clause and the field contain %, how to escape?

peter tong
Ranch Hand

Joined: Mar 15, 2008
Posts: 234
e.g. the filedA contain a value "test%2"

then in preparedstatement,


how to pass the 'xxx' so it can retrieve all field value with leading 'test%'? (that is, escape the % after test)
Martin Vajsar
Bartender

Joined: Aug 22, 2010
Posts: 2333
    
    2

According to this document (see the bottom of the page), you can define your own escape character:


Apparently the exact syntax for specifying the escape character differs among databases and JDBC provides this mechanism to abstract from these differences. I've never used that myself, though; I don't need database agnostic code and therefore always use the native syntax of my database.

You can use whatever escape character you wish. I didn't go for the obvious backslash, since it has to be escaped in Java String literals themselves, which makes somewhat less obvious what is actually happening.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Preparedstatement with like where clause and the field contain %, how to escape?
 
Similar Threads
PreparedStatement-Statement
Special characters in hql
Insert into PrepareStatement Using Field Name
with Java: how to update a row in sybase with string having a single quote
how to deal with the special character in java and oracle!!!