Hitachi

Hitachi Advanced Database Application Development Guide


7.3.2 Using the getConnection method of the DataSource class to connect to the HADB server

Database connection (connection to the HADB server) using DataSource and JNDI can now be used by the JDBC 2.0 Optional Package.

Although it is not essential to use JNDI, using it offers the benefit that you need to specify the connection information only once. Because the DataSource class interface definition and JNDI are not included in JDK as standard features, you must obtain them from the JavaSoft web site when you develop an application program.

The following explains the procedure for using DataSource and JNDI to connect to the HADB server.

To connect to the HADB server:
  1. Generate the DataSource object.

  2. Set up the connection information.

  3. Register the DataSource object into JNDI.

  4. Get the DataSource object from JNDI.

  5. Connect to the HADB server.

If you are not using JNDI, steps 3 and 4 are not necessary.

If you are using JNDI, steps 1 through 3 need to be executed only once. Thereafter, you can connect to the HADB server by performing only steps 4 and 5. Once you have performed step 4, you can change the connection information as necessary.

Organization of this subsection

(1) Generating the DataSource object

Generate the DataSource class object to be provided by the JDBC driver.

The DataSource class name of the JDBC driver, which is necessary for generating the DataSource class object, is AdbDataSource.

Shown below is an example of generating the DataSource class object:

com.hitachi.hadb.jdbc.AdbDataSource  ds = null ;
ds = new com.hitachi.hadb.jdbc.AdbDataSource() ;

(2) Setting up the connection information

Call the method for setting up connection information for the DataSource object, and set up the connection information. There is also a method for acquiring the connection information, which you can use to check the current connection information. For details about the connection information setup and acquisition methods, see 10.5 Connection information setup and acquisition interface.

(3) Registering the DataSource object into JNDI

Register the DataSource object into JNDI.

JNDI can select from among several service providers, depending on the execution environment.

Shown below is an example of registering the DataSource object into JNDI (this example is for Windows). In the registration example, the File System service provider, which is one of the service providers, is used. For details about other service providers, see the JNDI documentation.

// Generate DataSource class object to be provided by JDBC driver
com.hitachi.hadb.jdbc.AdbDataSource  ds;
ds = new com.hitachi.hadb.jdbc.AdbDataSource();
 
// Set connection information
        :
// Get system properties
Properties sys_prop = System.getProperties() ;
 
// Set properties of File System service provider
sys_prop.put(Context.INITIAL_CONTEXT_FACTORY,
             "com.sun.jndi.fscontext.RefFSContextFactory");
 
// Set directory to be used by File System service provider
// (Register under c:\JNDI_DIR.)
sys_prop.put(Context.PROVIDER_URL, "file:c:\\" + "JNDI_DIR");
 
// Update system properties
System.setProperties(sys_prop) ;
 
// Initialize JNDI
Context ctx = new InitialContext();
 
// Register DataSource class object to be provided by HADB server
// driver into JNDI. Use logical name jdbc/TestDataSource
ctx.bind("jdbc" + "\\" + "TestDataSource", ds);
    :

When you register the logical name to be registered into JNDI, the JDBC 2.0 specifications recommend that you register the logical name under a subcontext called jdbc (jdbc/TestDataSource in the registration example).

(4) Getting the DataSource object from JNDI

Get the DataSource object from JNDI.

Shown below is an acquisition example for the DataSource object (this an example is for Windows). This acquisition example uses the File System service provider, which is one of the service providers. For details about other service providers, see the JNDI documentation.

// Get system properties
Properties sys_prop = System.getProperties() ;
 
// Set properties of File System service provider
sys_prop.put(Context.INITIAL_CONTEXT_FACTORY,
             "com.sun.jndi.fscontext.RefFSContextFactory");
 
// Set directory to be used by File System service provider
// (Register under c:\JNDI_DIR.)
sys_prop.put(Context.PROVIDER_URL, "file:c:\\" + "JNDI_DIR");
 
// Update system properties
System.setProperties(sys_prop) ;
 
// Initialize JNDI
Context ctx = new InitialContext();
 
// Get object of local name jdbc/TestDataSource from JNDI
Object obj = ctx.lookup("jdbc" + "\\" + "TestDataSource") ;
 
// Cast retrieved object to DataSource class type
DataSource ds = (DataSource)obj;
     :

(5) Connecting to the HADB server

Call the getConnection method for the DataSource object. Shown below is an example of calling the getConnection method.

 DataSource ds
 
 // Get DataSource object from JNDI
                     :
 
// Issue getConnection method
Connection con = ds.getConnection();
  OR
Connection con = ds.getConnection("USERID", "PASSWORD");#
#

The method's arguments (authorization identifier and password) take priority over the connection information that was specified for the DataSource object. The JDBC driver throws an SQLException in the following cases:

  • Required connection information is not specified in the DataSource object.

  • Specified connection information is invalid.

  • Connection with the HADB server fails.

After you have obtained the DataSource object from JNDI, you can set up the connection information again, as necessary. In this case, you must cast the DataSource object to the DataSource class type provided by the JDBC driver before you set up the information. An example is shown below:

 DataSource ds
 com.hitachi.hadb.jdbc.AdbDataSource  adb_ds;
 
 // Get DataSource object from JNDI
                     :
 
 // Cast DataSource object to DataSource class type provided
 // by JDBC driver
 adb_ds = (com.hitachi.hadb.jdbc.AdbDataSource)ds;
 
 // Set up connection information again
          :