Nonstop Database, HiRDB Version 9 UAP Development Guide
SQLJ has no CONNECT or DISCONNECT statement. Therefore, connection to or disconnection from a HiRDB server is coded as Java instructions.
To connect to a HiRDB server, use the following coding using a connection context.
Define a class for the connection context. Class-name indicates a Java identifier. The defined class inherits sqlj.runtime.ConnectionContext.
#sql modifier context class-name ; |
Using the declared class, declare the connection context (as a Java variable declaration). Connection-context indicates a Java identifier.
modifier class-name connection-context ; |
Create a connection context object using a new operator. During this step, connection is made to the HiRDB server. For the connection parameters, describe the HiRDB server at the connection destination, port number, authorization identifier, and password in the same format as that used for JDBC.
connection-context = new class-name(connection-parameter) ; |
When the native interface is used, there are three ways of connecting to the HiRDB server:
These connection methods are described below.
connection-context = new class-name(connection-parameters); |
connection-context = new class-name(); |
#sql context Ctx;
String Userid=new String("user1");
String Passwd=new String("puser1");
String Host=new String("HiRDB_SV");
short port=22000;
Ctx con = new Ctx(:Userid,:Passwd,:Host,:port);
|
#sql [connection-context]{CONNECT USER :embedded variable USING :embedded variable};
or
#sql [connection-context]{CONNECT :embedded variable IDENTIFIED BY :embedded variable};
|
#sql [connection-context]{CONNECT};
|
#sql context Ctx;
String Userid=new String("user1");
String Passwd=new String("puser1");
Ctx con;
#sql [con] {CONNECT USER :Userid USING :Passwd };
|
connection-context = new class-name(connection-object); |
#sql context Ctx;
java sql.Connection con =
java.sql.DriverManager.getConnection("jdbc:hitachi:PrdbDrive://DBID=22200,
DBHOST=HiRDB_SV","user1","user1");
Ctx ctx = new Ctx(con);
|
To disconnect from the HiRDB server, invoke the close method for the connection context. Note that there is no reconnection method. To reconnect, create a new object.
connection-context.close() ; |
An example of invoking the close method for the connection context follows:
#sql context DeptContext;
...
{
DeptContext deptCtx = new DeptContext(deptURL,true);
#sql [deptCtx] { DELETE FROM TAB };
deptCtx.close();
}
|
When using the native interface edition, you can use the DISCONNECT statement instead of invoking the close method for the connection context.
#sql[connection-context]{DISCONNECT};
|
An example of the DISCONNECT statement follows:
#sql context Ctx;
Ctx con;
#sql[con]{CONNECT};
#sql[con]{DISCONNECT};
|
For the standard interface edition, the default connection context is assumed if no connection context is specified in an SQL statement.
To use the default connection context, a UAP must create a connection context in advance, and set it as the default connection context. Once the default connection context is set, it remains valid until the close() method for the default connection context is issued or a new connection context is set as the default connection context.
The default connection context is held by a variable inside the default connection context class (JP.co.Hitachi.soft.HiRDB.sqj.runtime.PrdbContext).
The default connection context has multiple constructors that have the different arguments described as follows.
To specify the URL of the connection destination, authorization identifier, and password, use the same format as is used for the JDBC driver of HiRDB.
In SQLJ, to use a constructor that includes a connection URL during the creation of a connection context, you must specify autoCommit, and specify TRUE to enable it and FALSE to disable it.
If the default connection context is created from the JDBC connection context, the autoCommit setting in the JDBC connection context is inherited.
import JP.co.Hitachi.soft.HiRDB.sqj.runtime.PrdbContext;
...
PrdbContext pctx = new PrdbContext(url,user,passwd,autoCommit);
PrdbContext.setDefaultContext(pctx);
|
import JP.co.Hitachi.soft.HiRDB.sqj.runtime.PrdbContext;
...
PrdbContext pctx = new PrdbContext(url,user,passwd,autoCommit);
PrdbContext.setDefaultContext(pctx);
...
pctx.close();
PrdbContext new_pctx = new PrdbContext(url.use,passwd,autoCommit);
PrdbContext.setDefaultContext(new_pctx);
|
JP.co.Hitachi.soft.HiRDB.sqj.runtime.PrdbContext.getDefaultContext();
void print_address(String name) throws SQLException;
{
String telno;
sqlj.runtime.ConnectionContext ctx;
ctx = JP.co.Hitachi.soft.HiRDB.sqj.runtime.PrdbContext.getDefaultContext();
#sql [ctx] { SELECT TELNO INTO :telno
FROM PERSON
WHERE :name = NAME } ;
}
|
For the native interface edition, the default connection context class is held in a variable of JP.co.Hitachi.soft.HiRDB.pdjpp.runtime.PrdbContext.
The default connection context class has the following constructors:
import JP.co.Hitachi.soft.HiRDB.pdjpp.runtime.PrdbContext;
:
PrdbContext pctx = new PrdbContext();
PrdbContext.setDefaultContext(pctx);
|
An example of releasing and resetting the default connection context follows:
import JP.co.Hitachi.soft.HiRDB.pdjpp.runtime.PrdbContext;
:
PrdbContext pctx = new PrdbContext(user,passwd,host,port);
PrdbContext.setDefaultContext(pctx);
:
pctx.close();
PrdbContext new_pctx = new PrdbContext(user,passwd,host,port);
PrdbContext.setDefaultContext(new_pctx);
|
JP.co.Hitachi.soft.HiRDB.pdjpp.runtime.PrdbContext.getDefaultContext();
A coding example in which the default context is implicitly specified follows:
void print_address(String name) throws SQLException;
{
String telno;
JP.co.Hitachi.soft.HiRDB.pdjpp.runtime.ConnectionContext ctx;
ctx = JP.co.Hitachi.soft.HiRDB.sqj.runtime.PrdbContext.getDefaultContext();
#sql [ctx] { SELECT TELNO INTO :telno FROM PERSON WHERE :name = NAME } ;
}
|
All Rights Reserved. Copyright (C) 2011, Hitachi, Ltd.