3.1. SQLJ Programs

A SQLJ program, like any other Java program, is divided up into blocks, and a SQLJ statement may appear anywhere that a normal Java statement may appear. All SQLJ statements begin with the language token #sql to differentiate those statements from other Java program statements.

There are two types of SQLJ statements:

  • Declarations

  • Executable statements

Declarations are used to declare iterators and connection contexts. Iterators are used to store result sets from SQL queries that may return more than one row, and connection contexts are used to establish database connections. Executable statements are used to execute embedded SQL statements and PL/SQL blocks. Any SQL statement supported by the JDBC driver may be embedded within a SQLJ executable statement. Executable statements may also contain host expressions, which are used to exchange information between the Java program and the database via Java variables.

Of course, in order to perform any type of database operation, your SQLJ program must first connect to the database. Let's plunge in!

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.