SQL/MX Programming Manual for Java
SQLJ Programming
HP NonStop SQL/MX Programming Manual for Java—523726-003
3-15
Explicit Connection Contexts
For other types of connection context constructors and methods that you can use to
instantiate a connection context object, see Connection Context Constructors and
Methods on page 3-10.
Associating an SQLJ Executable Clause with a Connection
Context
To associate an embedded SQL statement with an explicit connection context, place
the name of the connection context object inside square brackets [] (also known as a
context clause) within the SQLJ executable clause, after #sql and before the SQL
statement clause. For the syntax of the SQLJ executable clause, see Executable
Clause on page A-4. For example, the highlighted code sets the connection context for
the DELETE statement to ctx:
#sql [ctx] {DELETE FROM samdbcat.invent.partsupp
WHERE partnum = :partNum};
If you try to associate a connection context with an SQLJ executable clause that
contains a FETCH statement or an iterator conversion clause (CAST host-
expression), a warning occurs during SQLJ processing. To avoid that warning, do
not associate a connection context with a FETCH statement or iterator conversion
clause, even if you are using explicit connection contexts in the program. SQL/MX
does not generate a system-named module for FETCH statements and iterator
conversion clauses because SQL/MX never stores those statements in a profile. The
connection context associated with the SELECT statement that populated the iterator
is always associated with the FETCH statement or iterator conversion clause for that
iterator.
This example shows connection contexts in an SQLJ program:
import java.sql.*;
// Declaring connection context classes
#sql context SQLMXCtx;
#sql context OtherCtx;
public class MyProg
{
// Declaring and initializing connection context objects
private SQLMXCtx ctx1 = null;
private OtherCtx ctx2 = null;
public MyProg()
{
try
{
// Instantiating connection context objects
ctx1 = SQLMXCtx.getDefaultContext();
ctx2 = OtherCtx.getDefaultContext();
// Associating connection contexts with SQLJ clauses
#sql [ctx1] {DELETE FROM samdbcat.invent.partsupp