Enter your keyword


Configure Oracle DataSource in Tomcat

Here is the procedure, how to configure Oracle datasource in Tomcat and lookup the same.

     1. Get Oracle driver

Download Oracle driver from here and copy it to $TOMCATlib folder

     2. Create META-INF/context.xml and add following entry.

 <?xml version=”1.0″ encoding=”windows-1252″ ?>
<Context override=”true”>

    <Resource name=”jdbc/hr” auth=”Container”
        type=”javax.sql.DataSource” maxActive=”100″ maxIdle=”30″
        maxWait=”10000″ username=”hr” password=”welcome1″
        url=”jdbc:oracle:thin:@localhost:1521:orcl” />   

    3. web.xml configuration

<?xml version = ‘1.0’ encoding = ‘windows-1252’?>
<web-app xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
         xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd”
         version=”2.5″ xmlns=”http://java.sun.com/xml/ns/javaee”>
    <description>Oracle Datasource</description>

  4. Lookup datasource via context lookup service

Below class lookup datasource and established connection with Oracle DB

import javax.naming.Context;
import javax.naming.InitialContext;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class GetConnection {

    private static DataSource ds;

    public static DataSource getDS() {
        try {
            Context ctx = new InitialContext();
            ds = (DataSource)ctx.lookup(“java:comp/env/jdbc/hr”);
        } catch (NamingException e) {
        return ds;

    public static void main(String[] args) {
        try {

            DataSource ds = GetConnection.getDS();

            Connection connection = ds.getConnection();
        } catch (SQLException sqle) {


Leave a Reply

Your email address will not be published.