Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic