Java Reference
In-Depth Information
of the language. This has been done with a number of languages, including
COBOL
, C, and even Java. The following is an example of
SQLJ
in Java:
String name;
Date hiredate;
#sql {
SELECT emp_name, hire_date
INTO :name, :hiredate
FROM employee
WHERE emp_num = 28959
};
Inline
SQL
is quite elegant in that it integrates tightly with the language. Native
language variables can be passed directly to the
SQL
as parameters, and results
can be selected directly into similar variables. In a sense, the
SQL
becomes a fea-
ture of the language.
Unfortunately, inline
SQL
is not widely adopted and has some significant issues
keeping it from gaining any ground. First,
SQL
is not a standard. There are many
extensions to
SQL
and each only works with one particular database. This frag-
mentation of the
SQL
language makes it difficult to implement an inline
SQL
parser that is both complete and portable across database platforms. The second
problem with inline
SQL
is that it is often not implemented as a true language fea-
ture. Instead, a precompiler is used to first translate the inline
SQL
into proper
code for the given language. This creates problems for tools like integrated devel-
opment environments (
IDE
s) that might have to interpret the code to enable
advanced features like syntax highlighting and code completion. Code that con-
tains inline
SQL
may not even be able to compile without the precompiler, a
dependency that creates concerns around the future maintainability of the code.
One solution to the pains of inline
SQL
is to remove the
SQL
from the lan-
guage level, and instead represent it as a data structure (i.e., a string) in the appli-
cation. This approach is commonly known as Dynamic
SQL
.
Dynamic SQL
Dynamic
SQL
deals with some of the problems of inline
SQL
by avoiding the pre-
compiler. Instead,
SQL
is represented as a string type that can be manipulated just
like any other character data in a modern language. Because the
SQL
is repre-
sented as a string type, it cannot interact with the language directly like inline
SQL
can. Therefore, Dynamic
SQL
implementations require a robust
API
for setting
SQL
parameters and retrieving the resulting data.
