In this chapter we looked at how to use MySQL stored programs from within Java programs. Java programs access relational databases through the JDBC interfaces supported by the MySQL Connector/J driver.
We first reviewed the fundamentals of using JDBC to process
basic SQL—queries, updates, inserts, deletes, DDL, and utility
statements. We showed how to use the
PreparedStatement interface to execute SQL
statements that are repeatedly executed, possibly with variable query
parameters or DML inputs. Finally, we looked at JDBC structures for
implementing transaction and error handling.
JDBC fully supports stored programs through the
CallableStatement interface. Callable
statements support multiple result sets, and they support
INOUT parameters. The
ResultSetMetaData interface can be used to
determine the structure of result sets returned by stored programs if
this is not known in advance.
Stored programs are suitable for use in J2EE applications, and stored procedures can be invoked from within J2EE application servers such as JBoss, WebLogic, and WebSphere. We can use stored programs in J2EE applications wherever we might embed standard SQL calls—from servlets, session EJBs, or Bean Managed Persistence (BMP) EJBs. However, stored programs cannot easily be leveraged from within Container Managed Persistence (CMP) EJBs.
We can use stored procedures in ORM frameworks such as Hibernate, although doing so involves more work than letting Hibernate generate its own native ...