Oracle9i SQLJ Developer's Guide and Reference
Release 1 (9.0.1)

Part Number A90212-01
Go To Documentation Library
Home
Go To Product List
Solution Area
Go To Table Of Contents
Contents
Go To Index
Index

Go to previous page Go to next page

3
Basic Language Features

This chapter discusses basic SQLJ language features and constructs that you use in coding your application.

SQLJ statements always begin with a #sql token and can be broken into two main categories: 1) declarations, used for creating Java classes for iterators (similar to JDBC result sets) or connection contexts (designed to help you strongly type your connections according to the sets of SQL entities being used); and 2) executable statements, used to execute embedded SQL operations.

For more advanced topics, see Chapter 7, "Advanced Language Features".

This chapter discusses the following topics.

Overview of SQLJ Declarations

A SQLJ declaration consists of the #sql token followed by the declaration of a class. SQLJ declarations introduce specialized Java types into your application. There are currently two kinds of SQLJ declarations, iterator declarations and connection context declarations, defining Java classes as follows:

In any iterator or connection context declaration, you may optionally include the following clauses:

These are described in "Declaration IMPLEMENTS Clause" and in "Declaration WITH Clause".

Rules for SQLJ Declarations

SQLJ declarations are allowed in your SQLJ source code anywhere that a class definition would be allowed in standard Java. The only limitation is that you cannot have a declaration inside a method block under JDK 1.1.x. For example:

SQLJ declaration;   // OK (top level scope)

class Outer
{
   SQLJ declaration; // OK (class level scope)

   class Inner
   {
      SQLJ declaration; // OK (nested class scope)
   }

   void func()
   {
      SQLJ declaration; // OK in JDK 1.2.x; ILLEGAL in JDK 1.1.x (method block)
   }
}


Note:

As with standard Java, any public class should be declared in one of the following ways (this is a requirement if you are using the standard javac compiler provided with the Sun Microsystems JDK):

  • Declare it in a separate source file. The base name of the file should be the same as the class name.

or:

  • Declare it at class-level scope or nested-class-level scope. In this case, it may be advisable to use public static modifiers.

 

Iterator Declarations

An iterator declaration creates a class that defines a kind of iterator for receiving query data. The declaration will specify the column types of the iterator instances, which must match the column types being selected from the database table.

Basic iterator declarations use the following syntax:

#sql <modifiers> iterator iterator_classname (type declarations);

Modifiers are optional and can be any standard Java class modifiers such as public, static, etc. Type declarations are separated by commas.

There are two categories of iterators--named iterators and positional iterators. For named iterators, you specify column names and types; for positional iterators, you specify only types.

The following is an example of a named iterator declaration:

#sql public iterator EmpIter (String ename, double sal);

This statement results in the SQLJ translator creating a public EmpIter class with a String attribute ename and a double attribute sal. You can use this iterator to select data from a database table with corresponding employee name and salary columns of matching names (ENAME and SAL) and datatypes (CHAR and NUMBER).

Declaring EmpIter as a positional iterator, instead of a named iterator, would be done as follows:

#sql public iterator EmpIter (String, double);

For more information about iterators, see "Multi-Row Query Results--SQLJ Iterators".

Connection Context Declarations

A connection context declaration creates a connection context class, whose instances are typically used for database connections that use a particular set of SQL entities.

Basic connection context declarations use the following syntax:

#sql <modifiers> context context_classname;

As for iterator declarations, modifiers are optional and can be any standard Java class modifiers. The following is an example:

#sql public context MyContext;

As a result of this statement, the SQLJ translator creates a public MyContext class. In your SQLJ code you can use instances of this class to create database connections to schemas that include a desired set of entities, such as tables, views, and stored procedures. Different instances of MyContext might be used to connect to different schemas, but each schema might be expected, for example, to include an EMP table, a DEPT table, and a TRANSFER_EMPLOYEE stored procedure.

Declared connection context classes are an advanced topic and are not necessary for basic SQLJ applications that use only one interrelated set of SQL entities. In basic scenarios, you can use multiple connections by creating multiple instances of the sqlj.runtime.ref.DefaultContext class, which does not require any connection context declarations.

See "Connection Considerations" for an overview of connections and connection contexts.

For information about creating additional connection contexts, see "Connection Contexts".

Declaration IMPLEMENTS Clause

When you declare any iterator class or connection context class, you can specify one or more interfaces to be implemented by the generated class.

Use the following syntax for an iterator class:

#sql <modifiers> iterator iterator_classname implements intfc1,..., intfcN 
     (type declarations);

The portion implements intfc1,..., intfcN is known as the implements clause. Note that in an iterator declaration, the implements clause precedes the iterator type declarations.

Here is the syntax for a connection context declaration:

#sql <modifiers> context context_classname implements intfc1,..., intfcN;

The implements clause is potentially useful in either an iterator declaration or a connection context declaration, but is more likely to be useful in iterator declarations--particularly in implementing the sqlj.runtime.Scrollable or sqlj.runtime.ForUpdate interface. Scrollable iterators are supported in Oracle SQLJ (see "Scrollable Iterators"); positioned updates or deletes are not currently supported.

For more information about the implements clause, see "Use of the IMPLEMENTS Clause in Iterator Declarations" and "Use of the IMPLEMENTS Clause in Connection Context Declarations".


Note:

The SQLJ implements clause corresponds to the Java implements clause. 


The following example uses an implements clause in declaring a named iterator class (presume you have created a package, mypackage, that includes an iterator interface, MyIterIntfc).

#sql public iterator MyIter implements mypackage.MyIterIntfc 
     (String ename, int empno);

The declared class MyIter will implement the mypackage.MyIterIntfc interface.

This next example declares a connection context class that implements an interface named MyConnCtxtIntfc (presume it, too, is in the package mypackage).

#sql public context MyContext implements mypackage.MyConnCtxtIntfc; 

Declaration WITH Clause

In declaring a connection context class or iterator class, you can use a with clause to specify and initialize one or more constants to be included in the definition of the generated class. Most of this usage is standard, although Oracle adds one kind of extended functionality for iterator declarations.

Standard WITH Clause Usage

In using a with clause, the constants that are produced are always public static final. Use the following syntax for an iterator class:

#sql <modifiers> iterator iterator_classname with (var1=value1,..., varN=valueN)
     (type declarations);

The portion with (var1=value1,..., varN=valueN) is the with clause. Note that in an iterator declaration, the with clause precedes the iterator type declarations.

Where there is both a with clause and an implements clause, the implements clause must come first. Note that parentheses are used to enclose with lists, but not implements lists.

Here is the syntax for a connection context declaration using a with clause:

#sql <modifiers> context context_classname with (var1=value1,..., varN=valueN);

And here is an example:

#sql public context MyContext with (typeMap="MyPack.MyClass");

The declared class MyContext will define the attribute typeMap that will be public static final of the type String and initialized to the value "MyPack.MyClass". This value is the fully qualified class name of a ListResourceBundle implementation that provides the mapping between SQL and Java types for statements executed on instances of the MyContext class.

Here is another example (see the note about sensitivity below):

#sql public iterator MyAsensitiveIter with (sensitivity=ASENSITIVE) 
     (String ename, int empno);

This declaration sets the cursor sensitivity to ASENSITIVE for a named iterator class.

The following example uses both an implements clause and a with clause (see the note about holdability immediately below).

#sql public iterator MyScrollableIterator implements sqlj.runtime.Scrollable
     with (holdability=true) (String ename, int empno);

The implements clause must precede the with clause.

This declaration implements the interface sqlj.runtime.Scrollable and enables the cursor holdability for a named iterator class (but holdability is not currently meaningful to Oracle9i).

The following standard constants on iterator declarations are not supported in Oracle SQLJ. They mostly involve cursor states and can take only particular values, as follows:

An iterator declaration having a with clause that specifies updateColumns must also have an implements clause that specifies the sqlj.runtime.ForUpdate interface.

Oracle SQLJ supports the following standard constants on connection context declarations.

The following standard constants on connection context declarations are not currently supported in Oracle SQLJ:

Oracle-Specific WITH Clause Usage

In addition to standard with clause usage in a connection context declaration to associate a type map with the connection context class, Oracle allows you to use a with clause to associate a type map with the iterator class in an iterator declaration. Here is an example:

#sql iterator MyIterator with (typeMap="MyTypeMap") (Person pers, Address addr);

If you use Oracle-specific code generation (through the translator setting -codegen=oracle), and you use type maps in your application, then your iterator and connection context declarations must use the same type map(s). See "Coding Considerations and Limitations with Oracle-Specific Code Generation" for more information.

Overview of SQLJ Executable Statements

A SQLJ executable statement consists of the #sql token followed by a SQLJ clause, which uses syntax that follows a specified standard for embedding executable SQL statements in Java code. The embedded SQL operation of a SQLJ executable statement can be any SQL operation supported by your JDBC driver (such as DML, DDL, and transaction control).

Rules for SQLJ Executable Statements

A SQLJ executable statement must follow these rules:

SQLJ Clauses

A SQLJ clause is the executable part of a statement (everything to the right of the #sql token). This consists of embedded SQL inside curly braces, preceded by a Java result expression if appropriate (such as result below):

#sql { SQL operation };   // For a statement with no output, like INSERT
...
#sql result = { SQL operation };   // For a statement with output, like SELECT

A clause without a result expression, such as in the first example, is known as a statement clause. A clause that does have a result expression, such as in the second example, is known as an assignment clause.

A result expression can be anything from a simple variable that takes a stored-function return value, to an iterator that takes several columns of data from a multi-row SELECT (where the iterator can be an instance of an iterator class or subclass).

A SQL operation in a SQLJ statement can use standard SQL syntax only, or can use a clause with syntax specific to SQLJ (see Table 3-1 and Table 3-2 below).

For reference, Table 3-1 lists supported SQLJ statement clauses, and Table 3-2 lists supported SQLJ assignment clauses. Details of how to use the various kinds of clauses are discussed elsewhere, as indicated. The two entries in Table 3-1 are general categories for statement clauses that use standard SQL syntax or Oracle PL/SQL syntax, as opposed to SQLJ-specific syntax.

Table 3-1 SQLJ Statement Clauses  
Category  Functionality  More Information 

SELECT INTO clause 

Select data into Java host expressions. 

"Single-Row Query Results--SELECT INTO Statements" 

FETCH clause 

Fetch data from a positional iterator. 

"Using Positional Iterators" 

COMMIT clause 

Commit changes to the data. 

"Using Manual COMMIT and ROLLBACK" 

ROLLBACK clause 

Cancel changes to the data. 

"Using Manual COMMIT and ROLLBACK" 

SET TRANSACTION clause 

Use advanced transaction control for access mode and isolation level. 

"Advanced Transaction Control" 

procedure clause 

Call a stored procedure. 

"Calling Stored Procedures" 

assignment clause 

Assign values to Java host expressions. 

"Assignment Statements (SET)" 

SQL clause 

Use standard SQL syntax and functionality: UPDATE, INSERT, DELETE

Oracle9i SQL Reference 

PL/SQL block 

Use BEGIN..END or DECLARE..BEGIN..END anonymous block inside SQLJ statement. 

"PL/SQL Blocks in Executable Statements"

PL/SQL User's Guide and Reference 

Table 3-2 SQLJ Assignment Clauses  
Category  Functionality  More Information 

query clause 

Select data into a SQLJ iterator. 

"Multi-Row Query Results--SQLJ Iterators" 

function clause 

Call a stored function. 

"Calling Stored Functions" 

iterator conversion clause 

Convert a JDBC result set to a SQLJ iterator. 

"Converting from Result Sets to Named or Positional Iterators" 


Note:

A SQLJ statement is referred to by the same name as the clause that makes up the body of that statement. For example, an executable statement consisting of #sql followed by a SELECT INTO clause is referred to as a SELECT INTO statement. 


Specifying Connection Context Instances and Execution Context Instances

If you have defined multiple database connections and want to specify a particular connection context instance for an executable statement, use the following syntax:

#sql [conn_context_instance] { SQL operation };

"Connection Considerations" discusses connection context instances.

If you have defined one or more execution context instances (of the class sqlj.runtime.ExecutionContext) and want to specify one of them for use with an executable statement, use the following syntax (similar to that for connection context instances):

#sql [exec_context_instance] { SQL operation };

You can use an execution context instance to provide status or control of the SQL operation of a SQLJ executable statement. (This is an advanced topic.) For example, you can use execution context instances in multithreading situations where multiple operations are occurring on the same connection. See "Execution Contexts" for information.

You can also specify both a connection context instance and an execution context instance:

#sql [conn_context_instance, exec_context_instance] { SQL operation };


Notes:

  • Include square brackets around connection context instances and execution context instances--they are part of the syntax.

  • If you specify both a connection context instance and an execution context instance, the connection context instance must come first.

 

Executable Statement Examples

Examples of elementary SQLJ executable statements appear below. More complicated statements are discussed later in this chapter.

Elementary INSERT

The following example demonstrates a basic INSERT. The statement clause does not require any syntax specific to SQLJ.

Consider a subset of the standard EMP table:

CREATE TABLE EMP (
   ENAME VARCHAR2(10),
   SAL NUMBER(7,2) );

Use the following SQLJ executable statement (that uses only standard SQL syntax) to insert Joe as a new employee into the EMP table, specifying his name and salary.

#sql { INSERT INTO emp (ename, sal) VALUES ('Joe', 43000) };

Elementary INSERT with Connection Context or Execution Context Instances

The following examples use ctx as a connection context instance (an instance of either the default sqlj.runtime.ref.DefaultContext or a class that you have previously declared in a connection context declaration) and execctx as an execution context instance:

#sql [ctx] { INSERT INTO emp (ename, sal) VALUES ('Joe', 43000) };

#sql [execctx] { INSERT INTO emp (ename, sal) VALUES ('Joe', 43000) };

#sql [ctx, execctx] { INSERT INTO emp (ename, sal) VALUES ('Joe', 43000) };

A Simple SQLJ Method

This example demonstrates a simple method using SQLJ code, demonstrating how SQLJ statements interrelate with and are interspersed with Java statements. The SQLJ statement uses standard INSERT INTO table VALUES syntax supported by Oracle SQL. The statement also uses Java host expressions, marked by colons (:), to define the values. (Host expressions are used to pass data between your Java code and SQL instructions. They are discussed in "Java Host Expressions, Context Expressions, and Result Expressions".)

public static void writeSalesData (int[] itemNums, String[] itemNames)
       throws SQLException
{
  for (int i =0; i < itemNums.length; i++)
    #sql { INSERT INTO sales VALUES(:(itemNums[i]), :(itemNames[i]), SYSDATE) };
}


Notes:

  • The throws SQLException is required. For information about exception-handling, see "Exception-Handling Basics".

  • SQLJ function calls also use a VALUES token, but these situations are not related semantically.

 

PL/SQL Blocks in Executable Statements

PL/SQL blocks can be used within the curly braces of a SQLJ executable statement just as SQL operations can, as in the following example:

#sql {
   DECLARE
      n NUMBER;
   BEGIN
      n := 1;
      WHILE n <= 100 LOOP
         INSERT INTO emp (empno) VALUES(2000 + n);
         n := n + 1;
      END LOOP;
   END
};

This example goes through a loop that inserts new employees in the EMP table, creating employee numbers 2001-2100. (It presumes data other than the employee number will be filled in later.)

Simple PL/SQL blocks can also be coded in a single line:

#sql { <DECLARE ...> BEGIN ... END };

Using PL/SQL anonymous blocks within SQLJ statements is one way to use dynamic SQL in your application. (See "Dynamic SQL--DynamicDemo.sqlj" for a sample.) You can also use dynamic SQL directly through Oracle SQLJ extensions (see "Support for Dynamic SQL"), or through JDBC code within a SQLJ application (see "SQLJ and JDBC Interoperability").


Notes:

  • It is recommended that you not close a PL/SQL block with a semi-colon after the END. The parser will detect the end of the block when it encounters the closing curly brace of the SQLJ clause.

  • Remember that using PL/SQL in your SQLJ code would prevent portability to other platforms, because PL/SQL is Oracle-specific.

 

Java Host Expressions, Context Expressions, and Result Expressions

This section discusses three categories of Java expressions used in SQLJ code: host expressions, context expressions, and result expressions. Host expressions are the most frequently used and merit the most discussion. (Another category of expressions, called meta bind expressions, are used specifically for dynamic SQL operations and use syntax similar to that of host expressions. See "Support for Dynamic SQL".)

SQLJ uses Java host expressions to pass arguments between your Java code and your SQL operations. This is how you pass information between Java and SQL. Host expressions are interspersed within the embedded SQL operations in SQLJ source code.

The most basic kind of host expression, consisting of only a Java identifier, is referred to as a host variable.

A context expression specifies a connection context instance or execution context instance to be used for a SQLJ statement.

A result expression specifies an output variable for query results or a function return.

(Result expressions and the specification of connection context instances and execution context instances were first introduced in "Overview of SQLJ Executable Statements".)

Overview of Host Expressions

Any valid Java expression can be used as a host expression. In the simplest case, which is typical, the expression consists of just a single Java variable. Other kinds of host expressions include the following:

Java identifiers used as host variables or in host expressions can represent any of the following:

Local variables used in host expressions can be declared anywhere that other Java variables can be declared. Fields can be inherited from a superclass.

Java variables that are legal in the Java scope where the SQLJ executable statement appears can be used in a host expression in a SQL statement, presuming its type is convertible to or from a SQL datatype.

Host expressions can be input, output, or input-output.

See "Supported Types for Host Expressions" for information about data conversion between Java and SQL during input and output operations.

Basic Host Expression Syntax

A host expression is preceded by a colon (":"). If the desired mode of the host expression (input, output, or input-output) is not the default, then the colon must be followed (before the host expression itself) by IN, OUT, or INOUT, as appropriate. These are referred to as mode specifiers. The default is OUT if the host expression is part of an INTO-list or is the assignment expression in a SET statement. Otherwise, the default is IN. (When using the default, you can still include the mode specifier if desired.)

Any OUT or INOUT host expression must be assignable (an l-value, meaning something that can logically appear on the left side of an equals sign).

The SQL code that surrounds a host expression can use any vendor-specific SQL syntax; therefore, no assumptions can be made about the syntax when parsing the SQL operations and determining the host expressions. To avoid any possible ambiguity, any host expression that is not a simple host variable (in other words, that is more complex than a non-dotted Java identifier) must be enclosed in parentheses.

To summarize the basic syntax:

Syntax Notes

Usage Notes

For examples of Oracle SQLJ runtime evaluation of host expressions, see "Examples of Evaluation of Java Expressions at Runtime".

Examples of Host Expressions

The following examples will help clarify the preceding syntax discussion. (Some of these examples use SELECT INTO statements, which are described in "Single-Row Query Results--SELECT INTO Statements".)

  1. In this example, two input host variables are used--one as a test value for a WHERE clause, and one to contain new data to be sent to the database.

    Presume you have a database employee table EMP with an ENAME column for employee names and a SAL column for employee salaries.

    The relevant Java code that defines the host variables is also shown in the example.

    String empname = "SMITH";
    double salary = 25000.0;
    ...
    #sql { UPDATE emp SET sal = :salary WHERE ename = :empname };
    
    

    IN is the default, but you can state it explicitly as well:

    #sql { UPDATE emp SET sal = :IN salary WHERE ename = :IN empname };
    
    

    As you can see, ":" can immediately precede the variable when not using the IN token, but ":IN" must be followed by white space before the host variable.

  2. This example uses an output host variable in a SELECT INTO statement, where you want to find out the name of employee number 28959.

    String empname;
    ...
    #sql { SELECT ename INTO :empname FROM emp WHERE empno = 28959 };
    
    

    OUT is the default for an INTO-list, but you can state it explicitly as well:

    #sql { SELECT ename INTO :OUT empname FROM emp WHERE empno = 28959 };
    
    

    This looks in the EMPNO column of the EMP table for employee number 28959, selects the name in the ename column of that row, and outputs it to the empname output host variable, which is a Java string.

  3. This example uses an arithmetic expression as an input host expression. The Java variables balance and minPmtRatio are multiplied, and the result is used to update the minPayment column of the creditacct table for account number 537845.

    float balance = 12500.0;
    float minPmtRatio = 0.05;
    ...
    #sql { UPDATE creditacct SET minPayment = :(balance * minPmtRatio) 
           WHERE acctnum = 537845 };
    
    

    Or, to use the IN token:

    #sql { UPDATE creditacct SET minPayment = :IN (balance * minPmtRatio) 
           WHERE acctnum = 537845 };
    
    
  4. This example shows use of the output of a method call as an input host expression and also uses an input host variable. This statement uses the value returned by getNewSal() to update the SAL column in the EMP table for the employee (in the ENAME column) who is specified by the Java empname variable. Java code initializing the host variables is also shown.

    String empname = "SMITH";
    double raise = 0.1;
    ...
    #sql {UPDATE emp SET sal = :(getNewSal(raise, empname)) 
          WHERE ename = :empname};
    
    

Overview of Result Expressions and Context Expressions

A context expression is an input expression that specifies the name of a connection context instance or an execution context instance to be used in a SQLJ executable statement. Any legal Java expression that yields such a name can be used.

A result expression is an output expression used for query results or a function return. It can be any legal Java expression that is assignable, meaning that it can logically appear on the left side of an equals sign (this is sometimes referred to as an l-value).

The following examples can be used for either result expressions or context expressions:

Result expressions and context expressions appear lexically in the SQLJ space, unlike host expressions, which appear lexically in the SQL space (inside the curly brackets of a SQLJ executable statement). Therefore, a result expression or context expression must not be preceded by a colon.

Evaluation of Java Expressions at Runtime

This section discusses the evaluation of Java host expressions, connection context expressions, execution context expressions, and result expressions when your application executes.

Here is a simplified representation of a SQLJ executable statement that uses all these kinds of expressions:

#sql [connctxt_exp, execctxt_exp] result_exp = { SQL with host expression };

Java expressions can be used as any of the following:

For standard SQLJ generated code, evaluation of Java expressions does have side effects in a Java program because they are evaluated by Java, not by the SQL engine. Furthermore, the order of evaluation of these expressions can be critical if any of the expressions have side effects.

The following is a summary (for standard generated code) of the overall order of evaluation, execution, and assignment of Java expressions for each statement that executes during runtime.


Note:

The following discussion does not apply to Oracle-specific generated code, produced through the SQLJ translator -codegen=oracle setting. Specifically, behavior will differ for OUT, INOUT, or result expressions that have side effects. See "Oracle-Specific Code Generation (No Profiles)" for more information. 


  1. If there is a connection context expression, then it is evaluated immediately (before any other Java expressions are evaluated).

  2. If there is an execution context expression, then it is evaluated after any connection context expression, but before any result expression.

  3. If there is a result expression, then it is evaluated after any context expressions, but before any host expressions.

  4. After evaluation of any context or result expressions, host expressions are evaluated from left to right as they appear in the SQL operation. As each host expression is encountered and evaluated, its value is saved to be passed to SQL.

    Each host expression is evaluated once and only once.

  5. IN and INOUT parameters are passed to SQL, and the SQL operation is executed.

  6. After execution of the SQL operation, the output parameters--Java OUT and INOUT host expressions--are assigned output in order from left to right as they appear in the SQL operation.

    Each output host expression is assigned once and only once.

  7. The result expression, if there is one, is assigned output last.

"Examples of Evaluation of Java Expressions at Runtime", has a series of examples that clarifies this sequence and discusses a number of special considerations.


Note:

Host expressions inside a PL/SQL block are all evaluated together before any statements within the block are executed. They are evaluated in the order in which they appear, regardless of control flow within the block. 


Once the expressions in a statement have been evaluated, input and input-output host expressions are passed to SQL and then the SQL operation is executed. After execution of the SQL operation, assignments are made to Java output host expressions, input-output host expressions, and result expressions as follows:

  1. OUT and INOUT host expressions are assigned output in order from left to right.

  2. The result expression, if there is one, is assigned output last.

Note that during runtime all host expressions are treated as distinct values, even if they share the same name or reference the same object. The execution of each SQL operation is treated as if invoking a remote method, and each host expression is taken as a distinct parameter. Each input or input-output parameter is evaluated and passed as it is first encountered, before any output assignments are made for that statement, and each output parameter is also taken as distinct and is assigned exactly once.

It is also important to remember that each host expression is evaluated only once. An INOUT expression is evaluated when it is first encountered. When the output assignment is made, the expression itself is not re-evaluated, nor are any side-effects repeated.

In discussing the evaluation order of host expressions, several points must be highlighted, as covered in "Examples of Evaluation of Java Expressions at Runtime" below.

Examples of Evaluation of Java Expressions at Runtime

This section discusses some of the subtleties of how Java expressions are evaluated when your application executes, providing examples. (Some of these examples use SELECT INTO statements, which are described in "Single-Row Query Results--SELECT INTO Statements"; some use assignment statements, which are described in "Assignment Statements (SET)"; and some use stored procedure and function calls, which are described in "Stored Procedure and Function Calls".)

Prefix Operators Act Before Evaluation; Postfix Operators Act After Evaluation

When a Java expression contains a Java postfix increment or decrement operator, the incrementation or decrementation occurs after the expression has been evaluated. Similarly, when a Java expression contains a Java prefix increment or decrement operator, the incrementation or decrementation occurs before the expression is evaluated.

This is equivalent to how these operators are handled in standard Java code.

Consider the following examples.

Example 1: postfix operator

Example 2: postfix operators

Example 3: prefix and postfix operators

Example 4: postfix operator

Example 5: postfix operators

Example 6: postfix operator

IN versus INOUT versus OUT Makes No Difference in Evaluation Order

Host expressions are evaluated from left to right. Whether an expression is IN, INOUT, or OUT makes no difference in when it is evaluated; all that matters is its position in the left-to-right order.

Example 7: IN versus INOUT versus OUT

Expressions in PL/SQL Blocks Are Evaluated Before Statements Are Executed

Host expressions in a PL/SQL block are all evaluated in one sequence, before any have been executed.

Example 8: evaluation of expressions in a PL/SQL block

Example 9: evaluation of expressions in a PL/SQL block (with postfix)

Example 10: statements in one block versus separate SQLJ executable statements

Expressions in PL/SQL Blocks Are Always Evaluated Once Only

Each host expression is evaluated once, and only once, regardless of program flow and logic.

Example 11: evaluation of host expression in a loop

Example 12: evaluation of host expressions in conditional blocks

Output Host Expressions Are Assigned Left to Right, Before Result Expression

Remember that OUT and INOUT host expressions are assigned in order from left to right, and then the result expression, if there is one, is assigned last. If the same variable is assigned more than once, then it will be overwritten according to this order, with the last assignment taking precedence.


Note:

Some of these examples use stored procedure and function calls, whose syntax is explained in "Stored Procedure and Function Calls"


Example 13: multiple output host expressions referencing the same variable

Example 14: multiple output host expressions referencing the same object

Example 15: results assignment taking precedence over host expression assignment

Restrictions on Host Expressions

Do not use "in", "out", and "inout" as identifiers in host expressions unless they are enclosed in parentheses. Otherwise, they might be mistaken for mode specifiers. This is case-insensitive.

For example, you could use an input host variable called "in" as follows:

:(in)

or:

:IN(in)

Single-Row Query Results--SELECT INTO Statements

When only a single row of data is being returned, SQLJ allows you to assign selected items directly to Java host expressions inside SQL syntax. This is done using the SELECT INTO statement.

SELECT INTO Syntax

The syntax for a SELECT INTO statement is as follows:

#sql { SELECT expression1,..., expressionN  INTO :host_exp1,..., :host_expN  
       FROM table <optional clauses> };

where:

A SELECT INTO statement must return one, and only one, row of data, otherwise an error will be generated at runtime.

The default is OUT for a host expression in an INTO-list, but you can optionally state this explicitly:

#sql { SELECT column_name1, column_name2  INTO :OUT host_exp1, :OUT host_exp2  
       FROM table WHERE condition };

Trying to use an IN or INOUT token in the INTO-list will result in an error at translation time.


Notes:

  • Permissible syntax for expression1 through expressionN, the table, and the optional clauses is the same as for any SQL SELECT statement. For information about what is permissible in Oracle SQL, see the Oracle9i SQL Reference.

  • There can be any number of SELECT-list and INTO-list items, as long as they match--one INTO-list item per SELECT-list item, with compatible types.

 

Examples of SELECT INTO Statements

The examples below use a subset of the standard EMP table:

CREATE TABLE EMP (
   EMPNO NUMBER(4),
   ENAME VARCHAR2(10),
   HIREDATE DATE );

The first example is a SELECT INTO statement with a single host expression in the INTO-list:

String empname;
#sql { SELECT ename INTO :enpname FROM emp WHERE empno=28959 };

The second example is a SELECT INTO statement with multiple host expressions in the INTO-list:

String empname;
Date hdate;
#sql { SELECT ename, hiredate INTO :empname, :hdate FROM emp 
       WHERE empno=28959 };

Examples with Host Expressions in SELECT-List

It is legal to use Java host expressions in the SELECT-list as well as in the INTO-list.

For example, you can select directly from one host expression into another (though this is of limited usefulness):

...
#sql { SELECT :name1 INTO :name2 FROM emp WHERE empno=28959 };
...

More realistically, you might want to perform an operation or concatenation on the data selected, as in the following examples (assume Java variables were previously declared and assigned, as necessary):

...
#sql { SELECT sal + :raise INTO :newsal FROM emp WHERE empno=28959 };
...

...
#sql { SELECT :(firstname + " ") || emp_last_name INTO :name FROM myemp 
       WHERE empno=28959 };
...

In the second example, presume MYEMP is a table much like the standard EMP table but with an EMP_LAST_NAME column instead of an ENAME column. In the SELECT statement, firstname is prepended to " " (a single space), using a Java host expression and Java string concatenation (the + operator). This result is then passed to the SQL engine, which uses SQL string concatenation (the || operator) to append the last name.

SELECT INTO Error Conditions

Remember that SELECT INTO statements are intended for queries that return exactly one row of data only.

A SELECT INTO query that finds zero rows or multiple rows will result in an exception, as follows:

These exceptions are listed under "Runtime Messages". You can retrieve the SQL state through the getSQLState() method of the java.sql.SQLException class, as described in "Retrieving SQL States and Error Codes".

This is vendor-independent behavior that is specified in the SQLJ ISO standard. There is no vendor-specific error code in these cases (the error code equals 0).

Multi-Row Query Results--SQLJ Iterators

A large number of SQL operations are multi-row queries. Processing multi-row query-results in SQLJ requires a SQLJ iterator, which is generally a strongly typed version of a JDBC result set and is associated with the underlying database cursor. SQLJ iterators are used first and foremost to take query results from a SELECT statement.

Additionally, Oracle SQLJ offers extensions that allow you to use SQLJ iterators and result sets in the following ways:

For information about usage as stored function returns, see "Using Iterators and Result Sets as Stored Function Returns", after stored procedures and stored functions have been discussed. The other uses listed above are documented later in this section.

For information about advanced iterator topics, see "Iterator Class Implementation and Advanced Functionality". This section discusses how iterator classes are implemented and what advanced functionality is available, such as interoperability with JDBC result sets and subclassing of iterators.

Iterator Concepts

Using a SQLJ iterator declaration, as described in "Overview of SQLJ Declarations", results in a strongly typed iterator. This is the typical usage for iterators, and takes particular advantage of SQLJ semantics-checking features during translation.

It is also possible, and at times advantageous, to use weakly typed iterators. There are generic classes you can instantiate to use a weakly typed iterator.

This section primarily introduces features of strongly typed iterators, but concludes with a brief introduction to weakly typed iterators.

Introduction to Strongly Typed Iterators

Before using a strongly typed iterator object, you must declare an iterator class. An iterator declaration specifies a Java class that SQLJ constructs for you, where the class attributes define the types (and, optionally, the names) of the columns of data in the iterator.

A SQLJ iterator object is an instantiation of such a specifically declared iterator class, with a fixed number of columns of predefined type. This is as opposed to a JDBC result set object, which is a standard java.sql.ResultSet instance and can, in principle, contain any number of columns of any type.

When you declare an iterator, you specify either just the datatypes of the selected columns, or both the datatypes and the names of the selected columns:

The datatypes (and names, if applicable) that you declare determine how query results will be stored in iterator objects you instantiate from that class. SQL data retrieved into an iterator object are converted to the Java types specified in the iterator declaration.

When you query to populate a named iterator object, the names and datatypes of the SELECT-fields must match the names and types of the iterator columns (case-insensitive). The order of the SELECT-fields is irrelevant--all that matters is that each SELECT-field name matches an iterator column name. In the simplest case, the database column names directly match the iterator column names. For example, data from an ENAME column in a database table can be selected and put into an iterator ename column. Alternatively, you can use an alias to map a database column name to an iterator column name if the names differ. Furthermore, in a more complicated query, you can perform an operation between two columns and alias the result to match the corresponding iterator column name. (These last two cases are discussed in "Instantiating and Populating Named Iterators".)

Because SQLJ iterators are strongly typed, they offer the benefit of Java type-checking during the SQLJ semantics-checking phase.

As an example, consider the following table:

CREATE TABLE EMPSAL (
   EMPNO NUMBER(4),
   ENAME VARCHAR2(10),
   OLDSAL NUMBER(10),
   RAISE NUMBER(10) );

Given this table, you can declare and use a named iterator as follows.

Declaration:

#sql iterator SalNamedIter (int empno, String ename, float raise);

Executable code:

class MyClass {
   void func() throws SQLException {
      ...
      SalNamedIter niter;
      #sql niter = { SELECT ename, empno, raise FROM empsal };
    
      ... process niter ...
   }
}

This is a simple case where the iterator column names match the table column names. Note that the order of items in the SELECT statement does not matter when you use a named iterator--data is matched by name, not position.

When you query to populate a positional iterator object, the data is retrieved according to the order in which you select the columns. Data from the first column selected from the database table is placed into the first column of the iterator, and so on. The datatypes of the table columns must be convertible to the types of the iterator columns, but the names of the database columns are irrelevant, as the iterator columns have no names.

Given the EMPSAL table above, you can declare and use a positional iterator as follows.

Declaration:

#sql iterator SalPosIter (int, String, float);

Executable code:

class MyClass {
   void func() throws SQLException {
      ...
      SalPosIter piter;
      #sql piter = { SELECT empno, ename, raise FROM empsal };
    
      ... process piter ...
   }
}

Note that the order of the data items in the SELECT statement must be the same as in the iterator.

The processing differs between named iterators and positional iterators, as described in "Accessing Named Iterators" and "Accessing Positional Iterators".

General Iterator Notes

In addition to the preceding concepts, be aware of the following general notes about iterators:

Introduction to Weakly Typed Iterators

In case you would rather not declare an iterator class, Oracle SQLJ permits you to use a weakly typed kind of iterator. Such iterators are known as result set iterators. To use a plain (non-scrollable) result set iterator, instantiate the sqlj.runtime.ResultSetIterator class. To use a scrollable result set iterator, instantiate the sqlj.runtime.ScrollableResultSetIterator class. (Scrollable iterators are described in "Scrollable Iterators".)

The drawback to using result set iterators, compared to strongly typed iterators, is that SQLJ cannot perform as much semantics-checking for your queries.

For more information, see "Result Set Iterators".

General Steps in Using an Iterator

Five general steps are involved in using SQLJ named or positional iterator:

  1. Use a SQLJ declaration to define the iterator class (in other words, to define the iterator type).

  2. Declare a variable of the iterator class.

  3. Populate the iterator variable with the results from a SQL query, using a SELECT statement.

  4. Access the query columns in the iterator (how to accomplish this differs between named iterators and positional iterators, as explained below).

  5. When you finish processing the results of the query, close the iterator to release its resources.

Named Iterators Versus Positional Iterators Versus Result Set Iterators

There are advantages and appropriate situations for each kind of SQLJ iterator.

Named iterators allow greater flexibility. Because data selection into a named iterator matches SELECT-fields to iterator columns by name, you need not be concerned about the order in your query. This is less prone to error, as it is not possible for data to be placed into the wrong column. If the names do not match, the SQLJ translator will generate an error when it checks your SQL statements against the database.

Positional iterators offer a familiar paradigm and syntax to developers who have experience with other embedded-SQL languages. With named iterators you use a next() method to retrieve data, while with positional iterators you use FETCH INTO syntax similar to that of Pro*C, for example. (Each fetch implicitly advances to the next available row of the iterator before retrieving the next set of values.)

Positional iterators do, however, offer less flexibility than named iterators, because you are selecting data into iterator columns by position, instead of by name. You must be certain of the order of items in your SELECT statement. You also must select data into all columns of the iterator, and it is possible to have data written into the wrong iterator column if the type of that column happens to match the datatype of the table column being selected.

Access to individual data elements is also less convenient with positional iterators. Named iterators, because they store data by name, are able to have convenient accessor methods for each column (for example, there would be an ename() method to retrieve data from an ename iterator column). With positional iterators, you must fetch data directly into Java host expressions with your FETCH INTO statement, and the host expressions must be in the correct order.

Finally, if you do not want to declare strongly typed iterator classes for your queries, you can choose the alternative of using weakly typed result set iterators. Result set iterators are most convenient when converting JDBC code to SQLJ code. You must balance this consideration against the fact that result set iterators (either ResultSetIterator instances or ScrollableResultSetIterator instances) do not allow complete SQLJ semantics-checking during translation. With named or positional iterators, SQLJ verifies that SELECT-list types match the Java types into which the data will be materialized. With result set iterators, this is not possible. See "Result Set Iterators" for more information.

Comparative Iterator Notes

Be aware of the following notes regarding SQLJ iterators:

Using Named Iterators

When you declare a named iterator class, you declare the name as well as the datatype of each column of the iterator.

When you select data into a named iterator, the SELECT-fields must match the iterator columns in two ways:

The order in which attributes are declared in your named iterator class declaration is irrelevant. Data is selected into the iterator based on name alone.

A named iterator has a next() method to retrieve data row by row, and an accessor method for each column to retrieve the individual data items. The accessor method names are identical to the column names. (Unlike most accessor method names in Java, accessor method names in named iterator classes do not start with "get".) For example, a named iterator object with a column sal would have a sal() accessor method.


Note:

The following restrictions apply in naming the columns of a named iterator:

  • Column names cannot use Java reserved words.

  • Column names cannot have the same name as utility methods provided in named iterator classes, such as the next(), close(), getResultSet(), and isClosed() methods. For scrollable named iterators, this includes additional methods such as previous(), first(), and last(). (See "The Scrollable Interface" and "Scrollable Named Iterators".)

 

Declaring Named Iterator Classes

Use the following syntax to declare a named iterator class:

#sql <modifiers> iterator classname <implements clause> <with clause> 
     ( type-name-list );

In this syntax, modifiers is an optional sequence of legal Java class modifiers, classname is the desired class name for the iterator, and type-name-list is a list of the Java types and names equivalent to (convertible from) the column types and column names in a database table.

The implements clause and with clause are optional, specifying interfaces to implement and variables to define and initialize, respectively. These are discussed in "Declaration IMPLEMENTS Clause" and "Declaration WITH Clause".

Now consider the following table:

CREATE TABLE PROJECTS (
   ID NUMBER(4),
   PROJNAME VARCHAR(30),
   START_DATE DATE,
   DURATION NUMBER(3) );

You might declare the following named iterator for use with this table:

#sql public iterator ProjIter (String projname, int id, Date deadline);

This will result in an iterator class with columns of data accessible using the following provided accessor methods: projname(), id(), and deadline().


Note:

As with standard Java, any public class should be declared in one of the following ways (this is a requirement if you are using the standard javac compiler provided with the Sun Microsystems JDK):

  • Declare it in a separate source file. The base name of the file should be the same as the class name.

or:

  • Declare it at class-level scope or nested-class-level scope, with public static modifiers.

 

Instantiating and Populating Named Iterators

Declare a variable of the ProjIter positional iterator type from the preceding section and populate it with a SELECT statement.

Continuing to use the PROJECTS table and ProjIter iterator defined in the preceding section, note that there are columns in the table whose names and datatypes match the id and projname columns of the iterator, but you must use an alias and perform an operation to populate the deadline column of the iterator. Here is an example:

ProjIter projsIter;

#sql projsIter = { SELECT start_date + duration AS deadline, projname, id 
                   FROM projects WHERE start_date + duration >= sysdate };

This calculates a deadline for each project by adding its duration to its start date, then aliases the results as deadline to match the deadline iterator column. It also uses a WHERE clause so that only future deadlines are processed (deadlines beyond the current system date in the database).

Similarly, you must create an alias if you want to use a function call. Suppose you have a function MAXIMUM() that takes a DURATION entry and an integer as input and returns the maximum of the two. (For example, you could input a 3 to make sure each project has at least a three-month duration in your application.)

Now presume you are declaring your iterator as follows:

#sql public iterator ProjIter2 (String projname, int id, float duration);

You could use the MAXIMUM() function in your query, with an alias for the result, as follows:

ProjIter2 projsIter2;

#sql projsIter2 = { SELECT id, projname, maximum(duration, 3) AS duration 
                    FROM projects };

Generally, you must use an alias in your query for any SELECT-field whose name is not a legal Java identifier or does not match a column name in your iterator.

Remember that in populating a named iterator, the number of columns you select from the database can never be less than the number of columns in the iterator. The number of columns you select can be greater than the number of columns in the iterator (unmatched columns are ignored), but this will generate a warning unless you have the SQLJ -warn=nostrict option set.

Accessing Named Iterators

Use the next() method of the named iterator object to step through the data that was selected into it. To access each column of each row, use the accessor methods generated by SQLJ, typically inside a while loop.

Whenever next() is called:

The following is an example of how to access the data of a named iterator, repeating the declaration, instantiation, and population used under "Instantiating and Populating Named Iterators".


Note:

Each iterator has a close() method that you must always call when you finish retrieving data from the iterator. This is necessary to close the iterator and free its resources. 


Presume the following iterator class declaration:

#sql public iterator ProjIter (String projname, int id, Date deadline);

Populate and then access an instance of this iterator class as follows:

// Declare the iterator variable
ProjIter projsIter;

// Instantiate and populate iterator; order of SELECT doesn't matter
#sql projsIter = { SELECT start_date + duration AS deadline, projname, id 
                   FROM projects WHERE start_date + duration >= sysdate };

// Process the results
while (projsIter.next()) {
   System.out.println("Project name is " + projsIter.projname());
   System.out.println("Project ID is " + projsIter.id());
   System.out.println("Project deadline is " + projsIter.deadline());
}  

// Close the iterator
projsIter.close();
...

Note the convenient use of the projname(), id(), and deadline() accessor methods to retrieve the data. Note also that the order of the SELECT items does not matter, nor does the order in which the accessor methods are used.

Remember, however, that accessor method names are created with the case exactly as in your declaration of the iterator class. The following will generate compilation errors.

Declaration:

#sql iterator Cursor1 (String NAME);

Executable code:

...
Cursor1 c1;
#sql c1 = { SELECT NAME FROM TABLE };
while (c1.next()) {
   System.out.println("The name is " + c1.name());
}
...

The Cursor1 class has a method called NAME(), not name(). You would have to use c1.NAME() in the System.out.println statement.

For a complete sample of using a named iterator, see "Named Iterator--NamedIterDemo.sqlj".

Using Positional Iterators

When you declare a positional iterator class, you declare the datatype of each column but not the column name. The Java types into which the columns of the SQL query results are selected must be compatible with the datatypes of the SQL data. The names of the database columns or SELECT-fields are irrelevant.

Because names are not used, the order in which you declare your positional iterator Java types must exactly match the order in which the data is selected.

To retrieve data from a positional iterator once data has been selected into it, use a FETCH INTO statement followed by an endFetch() method call to determine if you have reached the end of the data (as detailed under "Accessing Positional Iterators").

Declaring Positional Iterator Classes

Use the following syntax to declare a positional iterator class:

#sql <modifiers> iterator classname <implements clause> <with clause> 
     ( position-list );

In this syntax, modifiers is an optional sequence of legal Java class modifiers, and the position-list is a list of Java types compatible with the column types in a database table.

The implements clause and with clause are optional, specifying interfaces to implement and variables to define and initialize, respectively. These are discussed in "Declaration IMPLEMENTS Clause" and "Declaration WITH Clause".

Now consider the following table, a subset of the standard EMP table:

CREATE TABLE EMP (
   EMPNO NUMBER(4),
   ENAME VARCHAR2(10), 
   SAL NUMBER(7,2) );

And consider the following positional iterator declaration:

#sql public iterator EmpIter (String, int, float);

This example defines Java class EmpIter with unnamed String, int, and float columns. Note that the table columns and iterator columns are in a different order--the String corresponds to ENAME and the int corresponds to EMPNO. The order of the iterator columns determines the order in which you must select the data, as shown in "Instantiating and Populating Positional Iterators" below.


Note:

As with standard Java, any public class should be declared in one of the following ways (this is a requirement if you are using the standard javac compiler provided with the Sun Microsystems JDK):

  • Declare it in a separate source file. The base name of the file should be the same as the class name.

or:

  • Declare it at class-level scope or nested-class-level scope, with public static modifiers.

 

Instantiating and Populating Positional Iterators

Declare a variable of the EmpIter positional iterator type from the preceding section and populate it with a SELECT statement.

Instantiating and populating a positional iterator is no different than doing so for a named iterator, except that you must be certain that your SELECT-fields are in the proper order.

The three datatypes in the EmpIter iterator class are compatible with the types of the EMP table, but be careful how you select the data, because the order is different. The following will work, because the SELECT-fields are in the same order as the iterator columns, as declared above in "Declaring Positional Iterator Classes":

EmpIter empsIter;

#sql empsIter = { SELECT ename, empno, sal FROM emp };

Remember that in populating a positional iterator, the number of columns you select from the database must equal the number of columns in the iterator.

Accessing Positional Iterators

Access the columns defined by a positional iterator using SQL FETCH INTO syntax.

The INTO part of the command specifies Java host variables that receive the results columns. The host variables must be in the same order as the corresponding iterator columns. Use the endFetch() method provided with all positional iterator classes to determine whether the last fetch reached the end of the data.


Notes:

  • The endFetch() method initially returns true before any rows have been fetched, then returns false once a row has been successfully retrieved, then returns true again when a FETCH finds no more rows to retrieve. Therefore, you must perform the endFetch() test after the FETCH INTO statement. If your endFetch() test precedes the FETCH INTO statement, then you will never retrieve any rows, because endFetch() would be true before your first FETCH and you would immediately break out of the while loop.

  • The endFetch() test must be before the results are processed, however, because the FETCH does not throw a SQL exception when it reaches the end of the data, it just triggers the next endFetch() call to return true. If there is no endFetch() test before results are processed, then your code will try to process null or invalid data from the first FETCH attempt after the end of the data had been reached.

  • Each iterator has a close() method that you must always call once you finish retrieving data from it. This is necessary to close the iterator and free its resources.

 

The following is an example, repeating the declaration, instantiation, and population used under "Instantiating and Populating Positional Iterators" above.

Note that the Java host variables in the SELECT statement are in the same order as the columns of the positional iterator, which is mandatory.

First, presume the following iterator class declaration:

#sql public iterator EmpIter (String, int, float);

Populate and then access an instance of this iterator class as follows:

// Declare and initialize host variables
int empnum=0;
String empname=null;
float salary=0.0f;

// Declare an iterator instance
EmpIter empsIter;

#sql empsIter = { SELECT ename, empno, sal FROM emp };

while (true) {
   #sql { FETCH :empsIter INTO :empnum, :empname, :salary };
   if (empsIter.endFetch()) break;  // This test must be AFTER fetch,
                                    // but before results are processed.
   System.out.println("Name is " + empname);
   System.out.println("Employee number is " + empnum);
   System.out.println("Salary is " + salary);
}

// Close the iterator
empsIter.close();
...

The empname, empnum, and salary variables are Java host variables whose types must match the types of the iterator columns.

Do not use the next() method for a positional iterator. A FETCH operation calls it implicitly to move to the next row.


Note:

Host variables in a FETCH INTO statement must always be initialized because they are assigned in one branch of a conditional statement. Otherwise, you will get a compiler error indicating they may never be assigned. (FETCH can assign the variables only if there was a row to be fetched.) 


For a complete sample of using a positional iterator, see "Positional Iterator--PosIterDemo.sqlj".

Positional Iterator Navigation with the next() Method

The positional iterator FETCH clause discussed in the previous section performs a movement--an implicit next() call--before it populates the host variables (if any). As an alternative, Oracle SQLJ supports using a special FETCH syntax in conjunction with explicit next() calls in order to use the same movement logic as with JDBC result sets and SQLJ named iterators. Using this special FETCH syntax, the semantics differ--there is no implicit next() call before the INTO-list is populated.

See "From JDBC Result Sets to SQLJ Iterators -- FETCH CURRENT Syntax" for more information.

Using Iterators and Result Sets as Host Variables

SQLJ supports SQLJ iterators and JDBC result sets as host variables, as illustrated in the examples below.


Notes:

  • Additionally, SQLJ supports iterators and result sets as return variables for stored functions. This is discussed in "Using Iterators and Result Sets as Stored Function Returns".

  • The Oracle JDBC drivers do not currently support result sets as input host variables. There is a setCursor() method in the OraclePreparedStatement class, but it raises an exception at runtime if called.

 

As you will see from the following examples, using iterators and result sets is fundamentally the same, with differences in declarations and in accessor methods to retrieve the data.

For the examples in this section, consider the following tables--subsets of the standard DEPT and EMP tables:

CREATE TABLE DEPT (
   DEPTNO NUMBER(2),
   DNAME VARCHAR2(14) );

CREATE TABLE EMP (
   EMPNO NUMBER(4),
   ENAME VARCHAR2(10), 
   SAL NUMBER(7,2), 
   DEPTNO NUMBER(2) );

Example: Use of Result Set as OUT Host Variable

This example uses a JDBC result set as an output host variable.

...
ResultSet rs;
...
#sql { BEGIN
          OPEN :OUT rs FOR SELECT ename, empno FROM emp;
       END };

while (rs.next())
{
   String empname = rs.getString(1);
   int empnum = rs.getInt(2);
}
rs.close();
...

This example opens the result set rs in a PL/SQL block to receive data from a SELECT statement, selects data from the ENAME and EMPNO columns of the EMP table, then loops through the result set to retrieve data into local variables.

Example: Use of Iterator as OUT Host Variable

This example uses a named iterator as an output host variable.

Declaration:

#sql public <static> iterator EmpIter (String ename, int empno);

(The public modifier is required, and static may be advisable if your declaration is at class level or nested-class level.)

Executable code:

...
EmpIter iter;
...
#sql { BEGIN
          OPEN :OUT iter FOR SELECT ename, empno FROM emp;
       END };

while (iter.next())
{
   String empname = iter.ename();
   int empnum = iter.empno();
   
   ...process/output empname and empnum...
}
iter.close();
...

This example opens the iterator iter in a PL/SQL block to receive data from a SELECT statement, selects data from the ENAME and EMPNO columns of the EMP table, then loops through the iterator to retrieve data into local variables.

Example: Use of Iterator as OUT Host Variable for SELECT INTO

This example uses a named iterator as an output host variable, taking data through a SELECT INTO statement. (OUT is the default for host variables in an INTO-list. For information about SELECT INTO statements and syntax, see "Single-Row Query Results--SELECT INTO Statements".)

Declaration:

#sql public <static> iterator ENameIter (String ename);

(The public modifier is required, and static may be advisable if your declaration is at class level or nested-class level.)

Executable code:

...
ENameIter enamesIter;
String deptname;
...

#sql { SELECT dname, cursor 
      (SELECT ename FROM emp WHERE deptno = dept.deptno)
       INTO :deptname, :enamesIter FROM dept WHERE deptno = 20 };

System.out.println(deptname);
while (enamesIter.next())
{
   System.out.println(enamesIter.ename());
}
enamesIter.close();
... 

This example uses nested SELECT statements to accomplish the following:

In most cases, using SELECT INTO is more convenient than using nested iterators if you are retrieving a single row in the outer SELECT, although that option is also available as discussed below (such as in "Example: Named Iterator Column in a Positional Iterator"). Also, with nested iterators, you would have to process the data to determine how many rows there are in the outer SELECT. With SELECT INTO you are assured of just one row.

Using Iterators and Result Sets as Iterator Columns

Oracle SQLJ includes extensions that allow iterator declarations to specify columns of type ResultSet or columns of other iterator types declared within the current scope. In other words, iterators and result sets can exist within iterators in Oracle SQLJ. These column types are used to retrieve a column in the form of a cursor. This is useful for nested SELECT statements that return nested table information.

The following examples are functionally identical--each uses a nested result set or iterator (result sets or iterators in a column within an iterator) to print all the employees in each department in the DEPT table. The first example uses result sets within a named iterator, the second example uses named iterators within a named iterator, and the third example uses named iterators within a positional iterator.

Here are the steps:

  1. Select each DNAME (department name) from the DEPT table.

  2. Do a nested SELECT into a cursor to get all employees from the EMP table for each department.

  3. Put the department names and sets of employees into the outer iterator (iter), which has a name column and an iterator column. The cursor with the employee information for any given department goes into the iterator column of that department's row of the outer iterator.

  4. Go through a nested loop that, for each department, prints the department name and then loops through the inner iterator to print all employee names for that department.

Example: Result Set Column in a Named Iterator

This example uses a column of type ResultSet in a named iterator.

Declaration:

#sql iterator DeptIter (String dname, ResultSet emps);

Executable code:

...
DeptIter iter;
...
#sql iter = { SELECT dname, cursor 
             (SELECT ename FROM emp WHERE deptno = dept.deptno) 
              AS emps FROM dept };

while (iter.next())
{
   System.out.println(iter.dname());
   ResultSet enamesRs = iter.emps();
   while (enamesRs.next())
   {
      String empname = enamesRs.getString(1);
      System.out.println(empname);
   }
   enamesRs.close();
}
iter.close();
...

Example: Named Iterator Column in a Named Iterator

This example uses a named iterator that has a column whose type is that of a previously defined named iterator (nested iterators).

Declarations:

#sql iterator ENameIter (String ename);
#sql iterator DeptIter (String dname, ENameIter emps);

Executable code:

...
DeptIter iter;
...
#sql iter = { SELECT dname, cursor 
             (SELECT ename FROM emp WHERE deptno = dept.deptno) 
              AS emps FROM dept };

while (iter.next())
{
   System.out.println(iter.dname());
   ENameIter enamesIter = iter.emps();
   while (enamesIter.next())
   {
      System.out.println(enamesIter.ename());
   }
   enamesIter.close();
}
iter.close();
...
Example: Named Iterator Column in a Positional Iterator

This example uses a positional iterator that has a column whose type is that of a previously defined named iterator (nested iterators). This uses the FETCH INTO syntax of positional iterators. This example is functionally equivalent to the previous two.

Note that because the outer iterator is a positional iterator, there does not have to be an alias to match a column name, as was required when the outer iterator was a named iterator in the previous example.

Declarations:

#sql iterator ENameIter (String ename);
#sql iterator DeptIter (String, ENameIter);

Executable code:

...
DeptIter iter;
...
#sql iter = { SELECT dname, cursor 
             (SELECT ename FROM emp WHERE deptno = dept.deptno) 
              FROM dept };
while (true)
{
   String dname = null;
   ENameIter enamesIter = null;
   #sql { FETCH :iter INTO :dname, :enamesIter };
   if (iter.endFetch()) break;
   System.out.println(dname);
   while (enamesIter.next())
   {
      System.out.println(enamesIter.ename());
   }
   enamesIter.close();
}
iter.close();
...

Assignment Statements (SET)

SQLJ allows you to assign a value to a Java host expression inside a SQL operation. This is known as an assignment statement and is accomplished using the following syntax:

#sql { SET :host_exp = expression };

The host_exp is the target host expression, such as a variable or array index. The expression could be a number, host expression, arithmetic expression, function call, or other construct that yields a valid result into the target host expression.

The default is OUT for a target host expression in an assignment statement, but you can optionally state this explicitly:

#sql { SET :OUT host_exp = expression };

Trying to use an IN or INOUT token in an assignment statement will result in an error at translation time.

The preceding statements are functionally equivalent to the following PL/SQL code:

#sql { BEGIN :OUT host_exp := expression; END };

Here is a simple example of an assignment statement:

#sql { SET :x = foo1() + foo2() };

This statement assigns to x the sum of the return values of foo1() and foo2() and assumes that the type of x is compatible with the type of the sum of the outputs of these functions.

Consider the following additional examples:

int i2;
java.sql.Date dat;
...
#sql { SET :i2 = TO_NUMBER(substr('750 etc.', 1, 3)) +
        TO_NUMBER(substr('250 etc.', 1, 3)) };
...
#sql { SET :dat = sysdate };
...

The first statement will assign to i2 the value 1000 (750 + 250). (The substr() calls take the first three characters of the strings, or '750' and '250'. The TO_NUMBER() calls convert the strings to the numbers 750 and 250.)

The second statement will read the database system date and assign it to dat.

An assignment statement is especially useful when you are performing operations on return variables from functions stored in the database. You do not need an assignment statement to simply assign a function result to a variable, because you can accomplish this using normal function call syntax as explained in "Stored Procedure and Function Calls". You also do not need an assignment statement to manipulate output from Java functions, because you can accomplish that in a normal Java statement. So you can presume that foo1() and foo2() above are stored functions in the database, not Java functions.

Stored Procedure and Function Calls

SQLJ provides convenient syntax for calling stored procedures and stored functions in the database. These procedures and functions could be written in Java, PL/SQL, or any other language supported by the database.

A stored function requires a result expression in your SQLJ executable statement to accept the return value, and can optionally take input, output, or input-output parameters as well.

A stored procedure does not have a return value but can optionally take input, output, or input-output parameters. A stored procedure can return output through any output or input-output parameter.


Note:

Remember that instead of using the following procedure-call and function-call syntax, you can optionally use JPublisher to create Java wrappers for PL/SQL stored procedures and functions, then call the Java wrappers as you would any other Java methods. JPublisher is discussed in "JPublisher and the Creation of Custom Java Classes". For additional information, see the Oracle9i JPublisher User's Guide


Calling Stored Procedures

Stored procedures do not have a return value but can take a list with input, output, and input-output parameters. Stored procedure calls use the CALL token, as shown below. The word "CALL" is followed by white space and then the procedure name. There must be a space after the CALL token to differentiate it from the procedure name. There cannot be a set of outer parentheses around the procedure call (this differs from the syntax for function calls, as explained in "Calling Stored Functions").

#sql { CALL PROC(<PARAM_LIST>) };

PROC is the name of the stored procedure, which can optionally take a list of input, output, and input-output parameters. PROC can include a schema or package name as well, such as SCOTT.MYPROC().

Presume that you have defined the following PL/SQL stored procedure:

CREATE OR REPLACE PROCEDURE MAX_DEADLINE (deadline OUT DATE) IS
   BEGIN
      SELECT MAX(start_date + duration) INTO deadline FROM projects;
   END;

This reads the table PROJECTS, looks at the START_DATE and DURATION columns, calculates start_date + duration in each row, then takes the maximum START_DATE + DURATION total and selects it into DEADLINE, which is an output parameter of type DATE.

In SQLJ, you can call this MAX_DEADLINE procedure as follows:

java.sql.Date maxDeadline;
...
#sql { CALL MAX_DEADLINE(:out maxDeadline) };

For any parameters, you must use the host expression tokens IN (optional/default), OUT, and INOUT appropriately to match the input, output, and input-output designations of the stored procedure. Additionally, the types of the host variables you use in the parameter list must be compatible with the parameter types of the stored procedure.


Note:

If you want your application to be compatible with Oracle7, do not include empty parentheses for the parameter list if the procedure takes no parameters. For example:

#sql { CALL MAX_DEADLINE };

not:

#sql { CALL MAX_DEADLINE() };
 

Calling Stored Functions

Stored functions have a return value and can also take a list of input, output, and input-output parameters. Stored function calls use the VALUES token, as shown below. This syntax consists of the word "VALUES" followed by the function call. In standard SQLJ, the function call must be enclosed in a set of outer parentheses, as shown. In Oracle SQLJ, the outer parentheses are optional. When using the outer parentheses, it does not matter if there is white space between the VALUES token and the begin-parenthesis. (A VALUES token can also be used in INSERT INTO table VALUES syntax supported by Oracle SQL, but these situations are unrelated semantically and syntactically.)

#sql result = { VALUES(FUNC(<PARAM_LIST>)) };

In this syntax, result is the result expression, which takes the function return value. FUNC is the name of the stored function, which can optionally take a list of input, output, and input-output parameters. FUNC can include a schema or package name, such as SCOTT.MYFUNC().

Referring back to the example in "Calling Stored Procedures", consider defining the stored procedure as a stored function instead, as follows:

CREATE OR REPLACE FUNCTION GET_MAX_DEADLINE RETURN DATE IS
   deadline DATE;
   BEGIN
      SELECT MAX(start_date + duration) INTO deadline FROM projects;
      RETURN deadline;
   END;

In SQLJ, you can call this GET_MAX_DEADLINE function as follows:

java.sql.Date maxDeadline;
...
#sql maxDeadline = { VALUES(GET_MAX_DEADLINE) };

The result expression must have a type compatible with the return type of the function.

In Oracle SQLJ, the following syntax (outer parentheses omitted) is also allowed:

#sql maxDeadline = { VALUES GET_MAX_DEADLINE };

For stored function calls, as with stored procedures, you must use the host expression tokens IN (optional/default), OUT, and INOUT appropriately to match the input, output, and input-output parameters of the stored function. Additionally, the types of the host variables you use in the parameter list must be compatible with the parameter types of the stored function.


Note:

If you want your stored function to be portable to non-Oracle environments, then you should use only input parameters in the calling sequence, not output or input-output parameters.

If you want your application to be compatible with Oracle7, then do not include empty parentheses for the parameter list if the function takes no parameters. For example:

#sql maxDeadline = { VALUES(GET_MAX_DEADLINE) };

not:

#sql maxDeadline = { VALUES(GET_MAX_DEADLINE()) };
 

Using Iterators and Result Sets as Stored Function Returns

SQLJ supports assigning the return value of a stored function to an iterator or result set variable, provided that the function returns a REF CURSOR type.

The following example uses an iterator to take a stored function return. Using a result set is similar.

Example: Iterator as Stored Function Return

This example uses an iterator as a return type for a stored function, using a REF CURSOR type in the process. (REF CURSOR types are described in "Support for Oracle REF CURSOR Types".)

Presume the following function definition:

CREATE OR REPLACE PACKAGE sqlj_refcursor AS
   TYPE EMP_CURTYPE IS REF CURSOR;
   FUNCTION job_listing (j varchar2) RETURN EMP_CURTYPE;
END sqlj_refcursor;

CREATE OR REPLACE PACKAGE BODY sqlj_refcursor AS 
   FUNCTION job_listing (j varchar) RETURN EMP_CURTYPE IS 
   DECLARE
      rc EMP_CURTYPE;
   BEGIN
      OPEN rc FOR SELECT ename, empno FROM emp WHERE job = j;
      RETURN rc;
   END;
END sqlj_refcursor;

Use this function as follows.

Declaration:

#sql public <static> iterator EmpIter (String ename, int empno);

(The public modifier is required, and static may be advisable if your declaration is at class level or nested-class level.)

Executable code:

EmpIter iter;
...
#sql iter = { VALUES(sqlj_refcursor.job_listing('SALES')) };

while (iter.next())
{
   String empname = iter.ename();
   int empnum = iter.empno();

   ... process empname and empnum ...
}
iter.close();
...

This example calls the job_listing() function to return an iterator that contains the name and employee number of each employee whose job title is "SALES". It then retrieves this data from the iterator.


Go to previous page Go to next page
Oracle
Copyright © 1996-2001, Oracle Corporation.

All Rights Reserved.
Go To Documentation Library
Home
Go To Product List
Solution Area
Go To Table Of Contents
Contents
Go To Index
Index