Variables in SQLJ applications
In application programs in other languages, you use host variables to pass data between the application program and the data server. In SQLJ programs, In SQLJ programs, you can use host variables or host expressions.
A host expression begins with a colon (:). The colon is followed by an optional parameter mode identifier (IN, OUT, or INOUT), which is followed by a parenthesized expression clause.
Host variables and host expressions are case sensitive.
A complex expression is an array element or Java expression that evaluates to a single value. A complex expression in an SQLJ clause must be surrounded by parentheses.
- The string __sJT_ is a reserved prefix for variable names that
are generated by SQLJ. Do not begin the following types of names
with __sJT_:
- Host expression names
- Java variable names that are declared in blocks that include executable SQL statements
- Names of parameters for methods that contain executable SQL statements
- Names of fields in classes that contain executable SQL statements, or in classes with subclasses or enclosed classes that contain executable SQL statements
- The string _SJ is a reserved suffix for resource files and classes that are generated by SQLJ. Avoid using the string _SJ in class names and input source file names.
Examples
Example: Declaring a Java identifier and using it in a SELECT statement:
In this example, the statement that
begins with #sql
has the same function as a SELECT
statement in other languages. This statement assigns the last name
of the employee with employee number 000010 to Java identifier empname
.
String empname;
…
#sql [ctxt]
{SELECT LASTNAME INTO :empname FROM EMPLOYEE WHERE EMPNO='000010'};
Example: Declaring a Java identifier and using it in a stored procedure call:
In this example,
the statement that begins with #sql
has the same
function as an SQL CALL statement in other languages. This statement
uses Java identifier empno
as
an input parameter to stored procedure A
. The keyword IN
,
which precedes empno
, specifies that empno
is
an input parameter. For a parameter in a CALL statement, IN
is
the default. The explicit or default qualifier that indicates
how the parameter is used (IN, OUT, or INOUT) must match the corresponding
value in the parameter definition that you specified in the CREATE
PROCEDURE statement for the stored procedure.
String empno = "0000010";
…
#sql [ctxt] {CALL A (:IN empno)};
Example: Using a complex expression as a host identifier:
This example uses complex expression (((int)yearsEmployed++/5)*500) as a host expression.
#sql [ctxt] {UPDATE EMPLOYEE
SET BONUS=:(((int)yearsEmployed++/5)*500) WHERE EMPNO=:empID};
- Evaluates each of the host expressions in the statement, from left to right, before assigning their respective values to the database.
- Evaluates side effects, such as operations with postfix operators, according to normal Java rules. All host expressions are fully evaluated before any of their values are passed to the data server.
- Uses Java rules for rounding and truncation.