Nonstop Database, HiRDB Version 9 UAP Development Guide
The JDBC2.0 Optional Package can now use database connections that use a DataSource object and JNDI.
Although use of JNDI is not required, the advantage of using it is that you only have to set up the connection information once. The standard JDK package does not include interface definitions for the DataSource class or JNDI, so you have to download these items from the JavaSoft Web site when you develop an AP.
To connect a database by using a DataSource object and JNDI:
If you are not using JNDI, steps 3 and 4 are not necessary.
If you are using JNDI, steps 1 to 3 need to be executed only once. Thereafter, you can connect to the database by performing only steps 4 and 5. Once you have performed step 4, you can change the connection information as necessary.
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 PrdbDataSource.
Below is an example of generating the DataSource class object:
JP.co.Hitachi.soft.HiRDB.JDBC.PrdbDataSource ds = null ; ds = new JP.co.Hitachi.soft.HiRDB.JDBC.PrdbDataSource() ; |
Call the method for setting up connection information for the DataSource object, and set up the connection information. Because there is also a method for acquiring connection information, you can use it to check the current connection information. For details about the connection information setup and acquisition methods, see 18.7 Connection information setup and acquisition interface.
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. JP.co.Hitachi.soft.HiRDB.JDBC.PrdbDataSource ds; ds = new JP.co.Hitachi.soft.HiRDB.JDBC.PrdbDataSource(); // 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 HiRDB 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 JDBC2.0 specifications recommend that you register the logical name under a subcontext called jdbc (jdbc/TestDataSource in the registration example).
Get the DataSource object from JNDI.
Shown below is a registration example for the DataSource object (this is an example for Windows). This registration 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; : |
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");# |
After getting the DataSource object from JNDI, 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 JP.co.Hitachi.soft.HiRDB.JDBC.PrdbDataSource hirdb_ds; // Get DataSource object from JNDI. : // Cast DataSource object to DataSource class type provided // by JDBC driver. dbp_ds = (JP.co.Hitachi.soft.HiRDB.JDBC.PrdbDataSource)ds; // Set up connection information again. : |
All Rights Reserved. Copyright (C) 2011, Hitachi, Ltd.