25.5. Calling Procedures

Many database systems support stored procedures, which are predefined sequences of SQL commands that you can call when you want to execute the function that the stored procedure defines. This is a very powerful facility with a lot of ramifications, so I'll only touch on the basics of how to use this here, just so that you are aware of it. JDBC provides support for this sort of capability through the java.sql.CallableStatement interface, which is derived from the PreparedStatement interface. You can obtain a CallableStatement reference corresponding to a stored procedure call by calling the prepareCall() method for a Connection object.

The argument to the prepareCall() method is a String object that defines a string in a format described as SQL escape syntax. The purpose of SQL escape syntax is to enable the driver to determine that this is not an ordinary SQL statement and needs to be transformed into a form that will be understood by the database system. This enables the idiosyncrasies of how stored procedures are called in different database systems to be accommodated, since it is up to the driver to recognize that the string is SQL escape syntax and transform it to the format required by the underlying database. Let's first consider the simplest possible form for a string to call a stored procedure:

"{call procedureName}"

The braces are always present. The procedure to be called has the name procedureName. Given that you have a Connection object connection ...

Get Ivor Horton's Beginning Java™ 2, JDK™ 5th 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.