SQL/MX Programming Manual for Java

SQLJ Programming
HP NonStop SQL/MX Programming Manual for Java523726-003
3-12
Explicit Connection Contexts
returns null, the program should load the JDBC/MX driver, create a JDBC connection,
and pass the JDBC connection object to the constructor of an explicit connection
context, as this example shows:
ctx = SQLMXCtx.getDefaultContext();
if (ctx == null) {
String driver = "jdbc-driver";
Class.forName(driver);
Connection jdbc =
DriverManager.getConnection("jdbc-url");
ctx = new SQLMXCtx(jdbc); /* Create an instance of the
* connection context object
* by invoking a connection
* context constructor */
}
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.
Using an Existing JDBC Connection Object
Use the CtxClassName(Connection conn) constructor to instantiate a connection
context object based on an existing JDBC Connection object.
Before instantiating the connection context object, explicitly load the JDBC/MX driver
and establish a JDBC connection. For more information, see Explicitly Loading the
JDBC/MX Driver on page 3-7.
Instantiate the connection context ctx by invoking the CtxClassName(Connection
conn) constructor and passing the JDBC Connection object conn to it:
ctx = new SQLMXCtx(conn);
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.
Setting a JDBC Connection as the Default Connection Context
Instead of using the default connection context provided by the SQLJ run-time
environment of SQL/MX, you can establish an explicit JDBC connection and specify it
as the default connection context for the program.
Before instantiating the connection context object, explicitly load the JDBC/MX driver
and establish a JDBC connection. For more information, see Explicitly Loading the
JDBC/MX Driver on page 3-7.
Instantiate the connection context ctx by invoking the CtxClassName(Connection
conn) constructor and passing the JDBC Connection object conn to it:
ctx = new SQLMXCtx(conn);