• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
peter tong
Ranch Hand
Posts: 250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gravity is a harsh mistress. But this tiny ad is pretty easy to deal with:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!