Oracle8i JDBC Developer's Guide and Reference Release 3 (8.1.7) Part Number A83724-01 |
Library | Service | Contents | Index

## Creating DataSource Objects Dynamically

If you want to bind only a single `DataSource` object in the namespace to be used for multiple database resources, you must do the following:

1. Bind the `DataSource` without specifying the URL, host, port, SID, or driver type. Thus, you execute the `bindds` tool with only the `-dstype` `jta` option, as follows:

       sess_sh -service jdbc:oracle:thin:@nsHost:5521:ORCL -user SCOTT -password TIGER
       & bindds /test/empDatabase -dstype jta

2. Retrieve the `DataSource` in your code. When you perform the lookup, you must cast the returned object to `OracleJTADataSource` instead of `DataSource`. The Oracle-specific version of the `DataSource` class contains methods to set the `DataSource` properties.

3. Set the following properties:

   - URL with the `OracleJTADataSource.setURL` method

   - Host, port, SID, and driver type if you did not set the URL with the following `OracleJTADataSource` methods: `setURL`, `setDatabaseName`,`setPortNumber`, and `setDriverType`

   - Database link if using two-phase commit engine with the `OracleJTADataSource.setDBLink` method

   - Username and password if need to provide authentication information for a remote database. This information could have been provided on the initial context environment or can be provided in the `getConnection` method. However, if you want to set it with the `OracleJTADataSource` methods, you can through the `setUser` and `setPassword` methods.

4. Retrieve the connection through the `OracleJTADataSource`.`getConnection` method as indicated in the other examples.

#### Example 17-4 Retrieving Generic DataSource

The following example retrieves a generically bound `DataSource` from the namespace using in-session lookup and initializes all relevant fields.

    //retrieve an in-session generic DataSource object
    OracleJTADataSource ds = (OracleJTADataSource)ic.lookup ("/test/genericDS");

    //set all relevant properties for my database
    //URL is for a local database so use the KPRB URL
    ds.setURL ("jdbc:oracle:kprb:");
    //Used in two-phase commit, so provide the fully qualified database link that
    //was created from the two-phase commit engine to this database
    ds.setDBLink("localDB.oracle.com");

    //Finally, retrieve a connection to the local database using the DataSource
    Connection conn = ds.getConnection ();

| Copyright © 1996-2000, Oracle Corporation. All Rights Reserved. |
Library | Service | Contents | Index