SQLJ connection technique 5: Use a previously created connection context

SQLJ connection technique 5 uses a previously created connection context to connect to the data source.

About this task

In general, one program declares a connection context class, creates connection contexts, and passes them as parameters to other programs. A program that uses the connection context invokes a constructor with the passed connection context object as its argument.

Example

Program CtxGen.sqlj declares connection context Ctx and creates instance oldCtx:
#sql context Ctx;
…
// Create connection context object oldCtx
Program test.sqlj receives oldCtx as a parameter and uses oldCtx as the argument of its connection context constructor:
void useContext(sqlj.runtime.ConnectionContext oldCtx)         
                                   // oldCtx was created in CtxGen.sqlj
{  
  Ctx myConnCtx=                            
    new Ctx(oldCtx);               // Create connection context object myConnCtx
                                   // from oldCtx
  #sql [myConnCtx] {SELECT LASTNAME INTO :empname FROM EMPLOYEE
     WHERE EMPNO='000010'};
                                   // Use myConnCtx for executing an SQL statement 
…
}