#sql { INSERT INTO branch VALUES (99, 'Central', '321 W. 5 Ave.', 'Vancouver', 7458222) };
Embedded SQL statements are known as static SQL statements because
their structure is determined at precompile time, i.e., before running
javac.
Java's version of embedded SQL is called SQLJ. A program with embedded
SQL statements needs a precompiler (translator), which translates the SQL
statements into calls to a DBMS-specific runtime library. In SQLJ,
a JDBC driver is used to access a database. For information on how
an embedded SQL program is compiled, click here.
For information on how a DBMS processes an SQL statement, click here.
The counterpart to embedded SQL is dynamic SQL. Unlike embedded SQL statements, dynamic SQL statements can be built "on the fly" at runtime rather than being defined at precompile time. This means that dynamic SQL does not require a precompiler. You can do almost everything with dynamic SQL as you can with embedded SQL. In addition, dynamic SQL allows you to make queries where the number of select-list items, number of input host variables, and the datatypes of the input host variables are unknown until runtime. Although dynamic SQL is more flexible than embedded SQL, there are benefits to using embedded SQL. For example, in embedded SQL, errors are checked earlier: at precompile time rather than at runtime. A program can have both embedded and dynamic SQL statements. For information on how a dynamic SQL statement is processed, click here.
As you will soon see, JDBC and thus call level interfaces have dynamic SQL capabilities. Like dynamic SQL, call level interfaces pass SQL statements to the DBMS for processing at runtime. However, with JDBC, there is no easy means to specify database object names at runtime because you cannot use ? in a prepared statement to represent a table or column name. You will need to use string manipulation routines to dynamically build the SQL string. The next section provides an example.
/*
* Constructs the query based on which columns were selected.
* Returns true is the query is valid and successfully executed by
* the executeQuery() method; otherwise returns false.
*/
private boolean constructQuery()
{
StringBuffer statement = new StringBuffer("SELECT");int numBoxesSelected = 0;
if (branchIDCBox.isSelected())
{
statement.append(" branch_id");
numBoxesSelected++;
}if (branchNameCBox.isSelected())
{
if (numBoxesSelected > 0)
{
statement.append(", branch_name");
}
else
{
statement.append(" branch_name");
numBoxesSelected++;
}
}if (branchAddrCBox.isSelected())
{
if (numBoxesSelected > 0)
{
statement.append(", branch_addr");
}
else
{
statement.append(" branch_addr");
numBoxesSelected++;
}
}if (branchCityCBox.isSelected())
{
if (numBoxesSelected > 0)
{
statement.append(", branch_city");
}
else
{
statement.append(" branch_city");
numBoxesSelected++;
}
}if (branchPhoneCBox.isSelected())
{
if (numBoxesSelected > 0)
{
statement.append(", branch_phone");
}
else
{
statement.append(" branch_phone");
numBoxesSelected++;
}
}statement.append(" FROM branch");
if (numBoxesSelected > 0)
{
return executeQuery(statement.toString());
}return false;
}
JDBC/Oracle Tutorial 3
Last updated May 8 2011