Java DataSource getJNDIConnectionByContainer(String dataSource)

Here you can find the source of getJNDIConnectionByContainer(String dataSource)

Description

GetConnection with jdbc/xxxDS

License

Open Source License

Parameter

Parameter Description
dataSource a parameter

Declaration

public static Connection getJNDIConnectionByContainer(String dataSource) 

Method Source Code


//package com.java2s;

import java.sql.Connection;

import javax.naming.InitialContext;
import javax.sql.DataSource;

public class Main {
    /**//from   w  w  w. j av a  2s .co  m
    * GetConnection with jdbc/xxxDS
    * @param dataSource
    * @return
    */
    public static Connection getJNDIConnectionByContainer(String dataSource) {

        try {
            Connection conn = null;
            InitialContext ctx = new InitialContext();
            if (ctx != null) {
                Object o = ctx.lookup(dataSource);
                DataSource ds = (DataSource) o;
                conn = ds.getConnection();
            }
            return conn;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

Related

  1. getDataSource(String service)
  2. getDataSourceCache(String hostName)
  3. getDBConnection(DataSource dataSource)
  4. getGlobalVariable(DataSource dataSource, String variable)
  5. getJdbcUrlFromDataSource(DataSource dataSource)
  6. getProductAllCount(DataSource ds, String key, String sql_allcount)
  7. getSchemaSeparator(DataSource dataSource)
  8. getTableColumnCount(DataSource dataSource, String selectedTable)
  9. getTables(DataSource datasource)