Chapter 11. Combining JDBC, SQLJ, and Dynamic SQL

JDBC is an extension to the Java language that may be used to access a database. It is far more difficult to write and understand than SQLJ, and unlike SQLJ, JDBC is not strongly typed. By using SQLJ, you get the compiler to do more of the work for you because it checks your SQL statements during compilation, whereas JDBC checks your SQL statements only when you actually run your program.

SQLJ enables you to develop your programs more rapidly, more concisely, and with fewer mistakes than if you were to use JDBC. The one advantage JDBC does have over SQLJ—at least through Version 8.1.7, but not for 9i and above—is the ability to run dynamic SQL statements. A dynamic SQL statement is one in which the database tables and columns referenced in the statement may be set when the program is executed, and are not necessarily known when the program is compiled. Static SQL statements remain fixed during the execution of a program, and the tables and columns involved are known when the program is compiled. This advantage of JDBC over SQLJ goes away in Version 9i, however. In SQLJ Version 9i, you can use SQLJ statements to execute dynamic SQL without having to use JDBC at all; I show you how to do this later in this chapter. If you are using SQLJ Version 8.1.7 or below, you can embed JDBC statements in your SQLJ programs to handle your dynamic SQL needs; I show you how to do that as well.

In the first section of this chapter, I discuss ...

Get Java Programming with Oracle SQLJ 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.