lookup Data Source in InitialContext - Java java.sql

Java examples for java.sql:DataSource

Description

lookup Data Source in InitialContext

Demo Code


import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.apache.log4j.Logger;

public class Main{
    //w  w  w  .j  a  v a 2 s  .co m
    public static DataSource lookupDataSource(String jndiName) {
        try {
            Context context = new InitialContext();
            final String PRIFIXSTR = "java:comp/env/";
            return (DataSource) context.lookup(PRIFIXSTR + jndiName);
        } catch (NamingException e) {
            throw new RuntimeException("Cannot find JNDI DataSource: ");
        }
    }
}

Related Tutorials