Showing posts with label Oracle DB connectivity. Show all posts
Showing posts with label Oracle DB connectivity. Show all posts

Thursday, 27 August 2015

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 $TOMCAT\lib 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"
        driverClassName="oracle.jdbc.driver.OracleDriver"
        url="jdbc:oracle:thin:@localhost:1521:orcl" />   
    </Context>



    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">
   
  <resource-ref>
    <description>Oracle Datasource</description>
    <res-ref-name>jdbc/hr</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>
</web-app>



  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) {
            e.printStackTrace();
        }
        return ds;
    }

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

            DataSource ds = GetConnection.getDS();

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

}




Wednesday, 26 August 2015

Get Oracle DB connection using Oracle driver

Here is an example to connect Oracle DB using Oracle driver.

1) Download Oracle JDBC Driver

Download Oracle JDBC Driver from  here

2) Set classpath of above jar

3) Use below code to connect Oracle DB

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ObtainDBConnection {
    public ObtainDBConnection() {
        super();
    }

    private static Connection getConnnection() {

        try {
            // Load oracle driver
            Class.forName("oracle.jdbc.driver.OracleDriver");
           
        } catch (ClassNotFoundException e) {

            System.out.println("Oracle JDBC Driver not found");
            e.printStackTrace();
            return null;

        }

        Connection connection = null;

        try {
           
            String connectionString = "jdbc:oracle:thin:@localhost:1521:orcl";
            String userName = "hr";
            String password = "welcome1";

            connection =
                    DriverManager.getConnection(connectionString,userName, password);

        } catch (SQLException e) {

            System.out.println("Oracle database connection couldn't be established. Please check DB details!!");
            e.printStackTrace();
            return null;

        }

        if (connection != null) {
            System.out.println("DB connection established successfully");
            return connection;
        } else {
            System.out.println("Failed to connect DB.");
            return null;
        }
    }
   
    public static void main(String args[]){
        Connection connection = ObtainDBConnection.getConnnection();
       
        System.out.println("Connection "+connection);
       
    }
}