get Connection from Context - Java JDBC

Java examples for JDBC:Connection

Description

get Connection from Context

Demo Code


//package com.java2s;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class Main {
    public static Connection getConnection() {

        DataSource ds = null;// www. j a  v  a 2s .  c  om
        Context context = null;

        try {
            context = new InitialContext();
            ds = (DataSource) context.lookup("java:comp/env/jdbc/opendb");
            return ds.getConnection();

        } catch (NamingException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;

    }
}

Related Tutorials