// Code to look up a datasource object for getting database connection

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

public class TestConnection {

  public TestConnection(){
    Connection connection =null;

    // Create the initial context. No JNDI properties are to be supplied here
    Context  initialContext = new InitialContext();
    DataSource dataSource = null;                // datasource object

    // Look up the datasource using the logical name  specified in the ejb-location tag in data-sources.xml
    // You must always cast or narrow the object that JNDI returns to the  DataSource, because the
    // JNDI lookup() method returns a Java object.

    dataSource = (DataSource)initialContext.lookup("jdbc/OracleDS");

    // Establishing db connection
    connection =  dataSource.getConnection();
  }
}
E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy