Java DataSource getConnection(CommonDataSource dataSource)

Here you can find the source of getConnection(CommonDataSource dataSource)

Description

get Connection

License

Apache License

Declaration

public static Connection getConnection(CommonDataSource dataSource) throws SQLException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.CommonDataSource;
import javax.sql.ConnectionPoolDataSource;
import javax.sql.DataSource;
import javax.sql.XADataSource;

public class Main {
    public static Connection getConnection(CommonDataSource dataSource) throws SQLException {
        Connection connection = null;
        if (dataSource != null) {
            if (dataSource instanceof ConnectionPoolDataSource) {
                connection = ((ConnectionPoolDataSource) dataSource).getPooledConnection().getConnection();
            } else if (dataSource instanceof DataSource) {
                connection = ((DataSource) dataSource).getConnection();
            } else if (dataSource instanceof XADataSource) {
                connection = ((XADataSource) dataSource).getXAConnection().getConnection();
            }/*from w ww . j  a  v  a  2s .  co m*/
        }

        return connection;
    }

    public static Connection getConnection(CommonDataSource dataSource, String userName, String password)
            throws SQLException {
        Connection connection = null;
        if (dataSource != null) {
            if (dataSource instanceof ConnectionPoolDataSource) {
                connection = ((ConnectionPoolDataSource) dataSource).getPooledConnection(userName, password)
                        .getConnection();
            } else if (dataSource instanceof DataSource) {
                connection = ((DataSource) dataSource).getConnection(userName, password);
            } else if (dataSource instanceof XADataSource) {
                connection = ((XADataSource) dataSource).getXAConnection(userName, password).getConnection();
            }
        }

        return connection;
    }
}

Related

  1. doInsert(DataSource ds, String tableName, int id, String value)
  2. doSelect(DataSource ds, String tableName, int id)
  3. execute(DataSource ds, String sql, Object... args)
  4. execute(String execute, DataSource datasource)
  5. executeQuery(DataSource dataSource, String... queries)
  6. getConnection(DataSource dataSource)
  7. getConnection(final DataSource ds, final boolean autoCommit)
  8. getConnectionFromDataSource(DataSource ds)
  9. getDatabaseName(DataSource dataSource)