To connect to a HiRDB server, use the following coding using a connection context.
(a) Defining a connection context class
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 ; |
(b) Declaring connection context
Using the declared class, declare the connection context (as a Java variable declaration). Connection-context indicates a Java identifier.
modifier class-name connection-context ; |
(c) Connecting to a HiRDB server
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) ; |
(d) Connecting to the HiRDB server when the native interface is used
When the native interface is used, there are three ways of connecting to the HiRDB server:
- Describing the connection as a Java instruction
- Using the CONNECT statement
- Using the JDBC connection object (Connection)
These connection methods are described below.
- 1. Describing the connection as a Java instruction
- Use the new operator to generate a connection context object. However, since JDBC is not being used, specify an authorization identifier, a password, a server name, and a port number in the connection parameters.
connection-context = new class-name(connection-parameters); |
- If no connection parameters are specified, the HiRDB server checks the client environment variables.
connection-context = new class-name(); |
- An example of creating a connection context follows:
#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); |
- 2. Using the CONNECT statement
- Specify an authorization identifier and a password in the connection parameters.
- The HiRDB server checks the client environment definitions for the port number and the server name.
#sql [connection-context]{CONNECT USER :embedded variable USING :embedded variable};
or
#sql [connection-context]{CONNECT :embedded variable IDENTIFIED BY :embedded variable}; |
- If connection parameters are not specified, the HiRDB server checks the client environment definitions.
#sql [connection-context]{CONNECT}; |
- An example of the CONNECT statement follows:
#sql context Ctx;
String Userid=new String("user1");
String Passwd=new String("puser1");
Ctx con;
#sql [con] {CONNECT USER :Userid USING :Passwd }; |
- 3. Using the JDBC connection object (Connection)
- Use the new operator to generate a connection context object. In the connection parameters, specify the JDBC connection object (java.sql.Connection).
connection-context = new class-name(connection-object); |
- An example of creating a connection context follows:
#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 version, 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}; |
(a) Standard interface version
For the standard interface version, 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.
- Constructor that has a JDBC connection object as an argument
- Constructor that has the URL of the connection destination, authorization identifier, password, and auto commit specification as arguments
- Constructor that has the specifications of the URL of the connection destination, Properties object, and autoCommit as arguments
- Constructor that has connection context as an argument
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.
- Creating and setting the default connection context
An example for creating and setting the default connection context follows:
import JP.co.Hitachi.soft.HiRDB.sqj.runtime.PrdbContext;
...
PrdbContext pctx = new PrdbContext(url,user,passwd,autoCommit);
PrdbContext.setDefaultContext(pctx); |
- Releasing and resetting the default connection context
An example of releasing and resetting the default connection context follows:
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); |
- Acquiring the default connection context
When the following method is invoked, the connection context can be acquired:
JP.co.Hitachi.soft.HiRDB.sqj.runtime.PrdbContext.getDefaultContext();
An example of specifying the default context follows:
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 } ;
} |
(b) Native interface version
For the native interface version, 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:
- Constructors with JDBC connection objects as arguments
- Constructors with the authorization identifier, password, server name, and port number of the connection destination as arguments
- Constructors with the authorization identifier and password specification of the connection destination as arguments
- Constructors with connection contexts as arguments
- Constructors without arguments
- Creating and setting the default connection context
An example of creating and setting the default connection context follows:
import JP.co.Hitachi.soft.HiRDB.pdjpp.runtime.PrdbContext;
:
PrdbContext pctx = new PrdbContext();
PrdbContext.setDefaultContext(pctx); |
- Releasing and resetting the default connection context
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); |
- Getting the default connection context
To get the default connection context, invoke the following method:
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 } ;
} |