Nonstop Database, HiRDB Version 9 UAP Development Guide
Database connection using DataSource and JNDI can now be used by the JDBC2.0 Optional Package.
Although it is not essential to use JNDI, using it offers a benefit in that you need to specify the connection information only once. Because DataSource class interface definition and JNDI are not included in JDK as standard features, you need to obtain them from the JavaSoft web site when developing application programs.
To connect to a database using DataSource and JNDI:
If you do not use JNDI, the operations in Steps 3 and 4 are unnecessary.
If you use JNDI, execute the operations in Steps 1 through 3 only once. Afterwards, you can connect to the database by performing the operations in Steps 4 and 5 only. Furthermore, after the operation in Step 4, you can modify the connection information as needed.
Generate the DataSource class objects provided by the JDBC driver.
The DataSource class name of the JDBC driver required to generate the DataSource class objects is JdbhDataSource.
A DataSource class object creation example follows:
JP.co.Hitachi.soft.HiRDB.JDBC.JdbhDataSource ds = null ; ds = new JP.co.Hitachi.soft.HiRDB.JDBC.JdbhDataSource() ; |
Call up a connection information setup method for the DataSource object and set up connection information. Because a connection information acquisition method can also be used, you can also check the current connection information. For details on the connection information setup/acquisition method, see 17.11 Connection information setup/acquisition interface.
Register the DataSource object in JNDI.
In JNDI, you can select a service provider from several that are available.
An example of obtaining a DataSource object in JNDI is shown as follows (for Windows). Note that this obtaining example uses File System, which is one of the service providers. For information on other service providers, see the JNDI documentation.
// Generate a DataSource class object provided by the JDBC driver. JP.co.Hitachi.soft.HiRDB.JDBC.JdbhDataSource ds; ds = new JP.co.Hitachi.soft.HiRDB.JDBC.JdbhDataSource(); // Specify connection information. ... // Obtain system properties. Properties sys_prop = System.getProperties() ; // Set up properties for the File System service provider. sys_prop.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory"); // Set up the directory to be used by the File System service provider. // (In this case, the directory is registered under c:\JNDI_DIR.) sys_prop.put(Context.PROVIDER_URL, "file:c:\\" + "JNDI_DIR"); // Update the system properties. System.setProperties(sys_prop) ; // Initialize JNDI. Context ctx = new InitialContext(); // Register a DataSource class object provided by the HiRDB driver in JNDI // under a logical name called jdbc/TestDataSource. ctx.bind("jdbc" + "\\" + "TestDataSource", ds); ... |
Note that the JDBC2.0 specification recommends that the logical name to be registered in JNDI be registered under a subcontext called jdbc (jdbc/TestDataSource in the registration example).
Obtain the DataSource object from JNDI.
An example of obtaining a DataSource object from JNDI is shown as follows (for Windows). Note that this obtaining example uses File System, which is one of service providers. For information on other service providers, see the JNDI documentation.
// Obtain system properties. Properties sys_prop = System.getProperties() ; // Set up properties for the File System service provider. sys_prop.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory"); // Set up the directory to be used by the File System service provider. // (In this case, the directory is registered under c:\JNDI_DIR.) sys_prop.put(Context.PROVIDER_URL, "file:c:\\" + "JNDI_DIR"); // Update the system properties. System.setProperties(sys_prop) ; // Initialize JNDI. Context ctx = new InitialContext(); // Obtain an object with a logical name jdbc/TestDataSource from JNDI. Object obj = ctx.lookup("jdbc" + "\\" + "TestDataSource") ; // Cast the extracted object into the DataSource class type. DataSource ds = (DataSource)obj; ... |
Invoke the getConnection method for the DataSource object.
An example of calling the getConnection method follows:
DataSource ds // Obtain a DataSource object from JNDI. ... // Issue the getConnection method. Connection con = ds.getConnection(); or Connection con = ds.getConnection("USERID", "PASSWORD");# |
You can set connection information again as necessary after the Datasource object is obtained from JNDI. In such a case, you must cast the Datasource object to the DataSource class type provided by the JDBC driver and then set the connection information.
DataSource ds JP.co.Hitachi.soft.HiRDB.JDBC.JdbhDataSource hirdb_ds; // Obtain a DataSource object from JNDI. ... // Cast the DataSource object to the DataSource class type provided by the JDBC driver. dbp_ds = (JP.co.Hitachi.soft.HiRDB.JDBC.JdbhDataSource)ds; // Reset the connection information. ... |
All Rights Reserved. Copyright (C) 2011, Hitachi, Ltd.