Escape Sequences
Escape sequences
allow JDBC programs to package certain database commands in a
database-independent manner. Since different databases implement
different features (especially scalar SQL functions) in different
ways, in order to be truly portable, JDBC needs to provide a standard
way to access at least a subset of that functionality.
We’ve already seen escape sequences twice: with the
various SQL date and time functions, and with the
CallableStatement
object.
A JDBC escape sequence consists of a
pair of curly braces, a keyword, and a set of parameters. Thus,
call
is the keyword for stored procedures, while
d
, t
, and ts
are keywords for dates and times. One keyword we
haven’t seen yet is escape
. This
keyword specifies the character that is used to escape wildcard
characters in a LIKE
statement:
stmt.executeQuery( "SELECT * FROM ApiDocs WHERE Field_Name like 'TRANS\_%' {escape '\'}");
Normally, the underscore ( _
) character is
treated as a single-character wildcard, while the percent sign
(%
) is the multiple-character wildcard. By
specifying the backslash (\
) as the escape
character, we can match on the underscore character itself. Note that
the escape
keyword can also be used outside
wildcard searches. For example, SQL string termination characters
(such as the single quote) need to be escaped when appearing within
strings.
The fn
keyword allows the use of internal scalar database functions. Scalar functions are a fairly standard component of most database ...
Get Java Enterprise in a Nutshell, Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.