× Please, log in to give us a feedback. Click here to login

You must be logged to download. Click here to login


MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation


MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to integrate JDBC with JNDI

The Java Naming and Directory Interface or JNDI is a registry service which provides a Java API to clients. See in this article how to create an application and configure the private resources.

The applications are larger and more complex nowadays; finding the application services and the objects becomes more and more difficult. After creating the distributed applications resources we must truthfully track each of software components in the location and the functionality. This task can swiftly possible by using JNDI. It is very rare that will we found out in a consolidated location process or the system. Now most often different people can store that types of information in databases or simple workgroup databases on file servers or the location filed and very often only these individuals know the location of this information and how to understand to it.

In general form an enterprise, or a single user (basically system can handle only one user at a time not more than one user can be controlled via the process) may use many dissimilar approaches to store the specific resources. Now some store files in well-defined directory pyramids within the file systems or the Java objects in a Remote Method Invocation registry process. Now some companies use LDAP (It is stands for lightweight Directory Access Protocol).

If we define it then we say it is an inconsequential client-server protocol for retrieving the directory services. The LDAP runs over Transmission Control Protocol/Internet Protocol or the supplementary connection oriented transfer services permitted the directory facilities to store the total employee information, as specimen like as phone numbers and e−mail addresses, or personalization data for the Web−site users.

Present Approach: At present each of these storage methods that signify different types of naming or the directory service process. Consequently the more types of the storage a procedure exists, the more difficult the task of pinpointing objects or resources. The abundant packing approaches make our job as a developer tougher because we must use a changed programming approach to retrieve the data from the sources. Now each has its own semantics as well as its own Application Programming Interface. For illustration, we retrieve information differently from a file system, a Remote Method Invocation registry, and a lightweight Directory Access Protocol −enabled directory. Nevertheless, Java Naming and Directory Interface provide a solution for the unifying access to many of the different technologies used to store the data.

It defines a public, stretchy, interface for accessing the communal naming and directory services. We can use the Java Naming and Directory Interface API to access a lightweight Directory Access Protocol −enabled directory, Remote Method Invocation registry, or file system with the same methods. Java Naming and Directory Interface is also a cornerstone of the Java2 Enterprise Edition platform.

Process to setting up a Web Application to use a Java Naming and Directory Interface Resource

In this article now I will arrange a web application to use a database connection pool that is provided by Oracle (Relational Database Management System Software) and it is defined as a Java Naming and Directory Interface resource. Commonly the following points are very important which we are discussing below.

Context Configuration: Now the resource is configured depending on what types of environment it is being used in here. A development, test, production environment will have the resources defined in different locations and configurations. Technically the Production environment configuration should override the Development or Test Environment configuration but to be sure please remove the context.xml file from META-INF.

Now I will define the variables in resource process:






Here the name variable is of the resource to be created and it will be used throughout our application as the reserve name. (The prefix should be set to 'jdbc/' for the Java Database Connectivity connection. In this article we can illustrate that we have used the jdbc/hrDS as the name.)



The username variable to be legitimate against the database to establish a connection process in our system.



The password variable that is used for password to be authenticated with the username against the database to start a connection process.



This variable is set for the maximum number of the database connections which is available in the pool. To set to ­1 for no limitation.



Here the maximum number of idle database connections to retain in the pool. This is Set -1 for without limitation.



The maxWait variable that is the maximum time to wait for a database construction to become obtainable in milliseconds. Naturally an exception will be thrown if the timeout is exceeded. This is Set to -1 to wait indefinitely.



The class name for the JDBC driver used to connect to the database.

Use oracle.jdbc.OracleDriver for Oracle.



The Java Database Connectivity connection Universal Resource Locator for the connecting to the database.



Optional human-readable description of the resource.

Table 1: Showing variable and their description

Now there are many Apache APIs that support the interaction with a relational database process. Creating a new connection for each user can be time consuming (often necessitating multiple seconds of clock time), in order to perform the database for transaction that might take milliseconds. The opening a connection per user can be unachievable in a publicly-hosted Internet application where the number of simultaneous connections to the users can be very large. Accordingly the developers often wish to share a “pool” of open connections among all the applications. The number of manipulators to actually perform a request at any given time is usually a very unimportant percentage of the total number of the active users. And during request processing is the only time when a database connection is required. The application itself logs into the Database Management System and handles any user to the account issues internally.

Here now we have defined the DBCP in our context.xml file.

Listing 1: Sample code is the part of the context.xml file.

  <Resource name="jdbc/hrDS" auth="Container"description="This is mrboolData Connection "type="javax.sql.DataSource"
  driverClassName="oracle.jdbc.OracleDriver"url="jdbc:oracle:bcei:@"username="scott" password="tiger" maxActive="20" maxIdle="1"maxWait="-1"/> 

Production Environment: Basically in the production process the DAWGs will need to define the DBCP resource in the servlet's context.xml file. Here that will make the resource available to the servlet as a global Java Naming and Directory Interface resource. We will need to add this step to the deployment checklist for the DAWGs to define the DBCP resource. They will add it to the $CATALINA_BASE/conf/context.xml file as shown below. We will need to remove the context.xml file from $WEB_ROOT/META-INF/context.xml if this was used in the test or development environments.

Listing 2: Sample showing JNDI configuration

  Java Naming and Directory Interface resource for MyApplication--><Resource name="jdbc/hrDS" auth="Container" description="mrboolDatabase Connection"type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"url="jdbc:oracle:bcei:@"username="scott" password="tiger" maxActive="20" maxIdle="1" maxWait="-1"/>

Web Descriptor: In your application's web descriptor add the resource reference to make available to the whole application.

Listing 3: Sample showing web descriptor

  <resource-ref> <description>Oracle Datasource</description><res-ref-name>jdbc/hrDS</res-ref-name><res-type>javax.sql.DataSource</res-type><res-auth>Container</res-auth>

Configure Connection Code: Next is to configure the connection code where the Initial Context will look up the name of the Java Naming and Directory Interface resource defined in the context.xml file and web.xml file. My resource's name is 'jdbc/hrDS' but the location is in the Java environment defined by 'java:comp/env/'.

Listing 4: Sample showing connection code

 public final class sql1
  private static String name = "java:comp/env/jdbc/hrDS"; // location of myresource private
  static DataSource dataSource; 
  dataSource = (DataSource) new InitialContext().lookup(name); 
  catch (NamingException e)
  throw new ExceptionInInitializerError(e);
  public static Connection getConnection() throws SQLException 
  return dataSource.getConnection();
  } //end of the code it is a part of the code that connect in process.

Example Code: This example uses the getConnection() to connect to the JDBC database successfully.

Listing 5: Sample showing establishing connection

  public class EmployeesRepositoryJDBC implements EmployeesRepository
  @Overridepublic student find(int id) 
  return null;
  public List<Employee> findAll() 
  Connection conn = null; 
  List<Employee> employees = new ArrayList<Employee>(); 
  try {
  conn = SQLUtils.getConnection(); 
  Statement stmt = conn.createStatement(); 
  ResultSet rs = stmt.executeQuery("select stu.roll, stu.sname, stu.course, stu.doj, stu.guardiansname,stu.contact" +" from stu"); 
  Employee student = new Employee(); 
  } catch (Throwable ignore) 
         } catch (Throwable ignore) {}
  return employees;}
  }  // This is also end of the code

If the working environment is not production then the DBCP can be configured as a private Java Naming and Directory Interface resource. It also expresses the resource in the context.xml file found in $WEB_ROOT/META-INF/context.xml in the Web Application Archive (WAR), or the corresponding directory if it is used in exploded WAR.

Point to Remember: When the WAR file is deployed, the context.xml file will be moved by the servlet container to $CATALINA_BASE/conf/[engine_name]/[hostname]/ and renamed to [WAR_FILE_NAME].xml.

Image 1: Show the context.xml file

Advantages of Java Naming and Directory Interface

Now I will discuss different types of important features of the Java Naming and Directory Interface These types of the advantages are mainly associated with point wise process.

  1. We only need to learn a single Application Programming Interface to access all kinds of directory service information, such as involved with the security authorizations, phone numbers, electronic and postal mail addresses, application preferences, network addresses, machine configurations, and other also.
  2. Java Naming and Directory Interface insulate the application from protocol and implementation details.
  3. We can use Java Naming and Directory Interface to read and write complete Java objects from directories path.
  4. We can create relationship with different types of directories, such as an LDAP (Lightweight Directory Access Protocol) directory with an NDS directory, and have the mixture appear to be one large, federated directory.
  5. The all types of applications can store factory objects and configuration variables in a global naming hierarchy using the Java Naming and Directory with Application Programming Interface.
  6. The Java Naming and Directory Interface are the Interfaces that provide a global memory hierarchy to accumulation and lookup arrangement objects.
  7. The Java Naming and Directory Interface will characteristically contain configured Factory objects.
  8. Here we give a typical illustration, where the application that will snatch a database Data Source to produce the Java Database Connectivity Connections. Since the configuration is left to the configuration files, and it is very easy for the application to change databases for different customers.

JNDI Architecture

The JNDI Architecture takes a great role to stimulate the system. We are showing different levels in the bellow architecture level scenario process. Here we show the following architecture levels.

  • First level Java Application
  • JNDI Application Interface
  • Naming Manager
  • JNDI implementation possibilities with the RMI, LDAP, DNS, NIS, NDS, CORBA

Image2: JNDI Architecture

Operations on a Java Naming and Directory Interface context






This function is used to retrieve a list of contents that are obtainable at the current context. This characteristically includes the names of the objects to be bound to the Java Naming and Directory Interface hierarchy as well as sub contexts.



This is the lookup function that moves from one context to another context and it is going from c:\ to c:\windows. We can also use lookup() to look up the objects guaranteed to the Java Naming and Directory Interface hierarchy. Naturally the return type of the lookup() function is the Java Naming and Directory Interface driver specific.



The rename function is very common way that is used to give a context a new name.



The function createSubcontext creates a subcontext from the current context that like c:\bcei \bar from the folder c:\ bcei directory.



This function is realistic for the subcontext from the current context and such as terminating c:\bcei\bar from the folder c:\bcei.



The bind function is to write approximately to the Java Naming and Directory Interface hierarchy at the current context. Now with the lookup function, Java Naming and Directory Interface and also the drivers accept different parameters to bind ().



The rebind function is the same operation as bind process, except it forces a bind even if there is previously approximately in the Java Naming and Directory Interface tree with the same name.


In the above discussion we get knowledge in the Database System (JDBC) with the Java Naming and Directory Interface system. It is accurate that without knowing the JDBC and the specific configuration model we cannot develop ourselves. We know that this article is very suitable for the JDBC based Java database software development (web base application) environment. The Java Naming and Directory Interface is a connection over identifying and directory services, that also delivers one collective interface to the directories. The Users who wants to access an LDAP (Lightweight Directory Access Protocol) directory can use the same Application Programming Interface. Java Naming and Directory Interface is a system for Java-based clients to interact with naming and directory systems. So, it is a clear conclusion that java JNDI is the standard JDBC database connectivity model in our new technology.

Website: www.techalpine.com Have 16 years of experience as a technical architect and software consultant in enterprise application and product development. Have interest in new technology and innovation area along with technical...

What did you think of this post?
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!

> More info about MrBool Credits
You must be logged to download.

Click here to login