Java Reference
In-Depth Information
 
listBindings(String name)— Enumerates the names bound in the named context, along with the
objects bound to them.
 
lookup(String name)— Retrieves the named object
Obviously, using JNDI improves the portability of an application by removing the need
to hard code a driver name and database name, in much the same way as a file
directory improves file access by overcoming the need to reference disk cyli nders and
sectors.
Deploying and Using a Basic Implementation of DataSource
A JDBC DataSource maintains information about how to locate the data as a set of
properties, such as the data-source name, the server name on which it resides, and
the port number.
Deploying a DataSource object consists of three tasks:
 
Creating an instance of the DataSource class
 
Setting its properties
 
Registering it with a JNDI naming service
The first step is to create the BasicDataSource object and set the ServerName,
DatabaseName, and Description properties:
com.dbaccess.BasicDataSource ds = new com.dbaccess.BasicDataSource();
ds.setServerName("jupiter");
ds.setDatabaseName("CUSTOMERS");
ds.setDescription("Customer database");
The BasicDataSource object is now ready to be registered with a JNDI naming
service. The JNDI API is used in the following way to create an InitialContext object
and to bind the BasicDataSource object ds to the logical name jdbc/customerDB:
Context ctx = new InitialContext();
ctx.bind("jdbc/customerDB", ds);
The prefix jdbc is a JNDI subcontext under the initial context, much like a subdirectory
under the root directory. The subcontext jdbc is reserved for logical names to be
bound to DataSource objects, so jdbc is always the first part of a logical name for a
data source.
To get a connection using a DataSource, simply create a JNDI Context, and supply
the name of the DataSource object to its lookup() method. The lookup() method
returns the DataSource object bound to that name, which can then be used to get a
Connection:
Search WWH ::




Custom Search