Example usage for javax.sql DataSource getConnection

List of usage examples for javax.sql DataSource getConnection

Introduction

In this page you can find the example usage for javax.sql DataSource getConnection.

Prototype

Connection getConnection() throws SQLException;

Source Link

Document

Attempts to establish a connection with the data source that this DataSource object represents.

Usage

From source file:com.uber.hoodie.cli.utils.HiveUtil.java

private static Connection getConnection(String jdbcUrl, String user, String pass) throws SQLException {
    DataSource ds = getDatasource(jdbcUrl, user, pass);
    return ds.getConnection();
}

From source file:com.qualogy.qafe.business.resource.rdb.DataSourceConnectionFactory.java

/**
 * returns a proxy database connection./*from w w  w  .j a  va  2 s  . c  om*/
 *
 * @param ds
 * @param dataId
 * @return
 * @throws SQLException
 */
public static Connection getProxyConnection(DataSource ds, DataIdentifier dataId) throws SQLException {
    Connection conn = ds.getConnection();

    //for OracleConnection
    if (((DelegatingConnection) conn).getInnermostDelegate() instanceof OracleConnection) {
        Properties properties = getOracleProxyConnectionProperties(dataId);

        OracleConnection oracleConnection = (OracleConnection) ((DelegatingConnection) conn)
                .getInnermostDelegate();
        oracleConnection.openProxySession(OracleConnection.PROXYTYPE_USER_NAME, properties);

        conn = oracleConnection;
    }

    return conn;
}

From source file:com.financial.tools.recorderserver.tools.util.DataLoader.java

protected static IDatabaseConnection getConnection(DataSource dataSource)
        throws DatabaseUnitException, SQLException {
    Connection connection = dataSource.getConnection();
    String url = connection.getMetaData().getURL();
    if (StringUtils.contains(url, ":h2:")) {
        return new H2Connection(connection, null);
    } else if (StringUtils.contains(url, ":mysql:")) {
        return new MySqlConnection(connection, null);
    } else if (StringUtils.contains(url, ":oracle:")) {
        return new OracleConnection(connection, null);
    } else {/*  ww w.  j  av a  2 s. co  m*/
        return new DatabaseConnection(connection);
    }
}

From source file:cn.org.citycloud.srdz.utils.Hibernates.java

private static String getJdbcUrlFromDataSource(DataSource dataSource) {
    Connection connection = null;
    try {// w w  w  .  j  av  a  2 s.c om
        connection = dataSource.getConnection();
        if (connection == null) {
            throw new IllegalStateException("Connection returned by DataSource [" + dataSource + "] was null");
        }
        return connection.getMetaData().getURL();
    } catch (SQLException e) {
        throw new RuntimeException("Could not get database url", e);
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
            }
        }
    }
}

From source file:com.cgdecker.guice.jdbc.Hsqldb.java

private static void setUpDatabase(DataSource dataSource) {
    Connection conn = null;/*from   w  ww .  j av  a  2  s  .  c  o m*/
    try {
        conn = dataSource.getConnection();
        PreparedStatement pS = conn.prepareStatement("SET DATABASE TRANSACTION CONTROL MVCC");
        pS.executeUpdate();
        pS.close();

        pS = conn.prepareStatement("DROP TABLE foo IF EXISTS");
        pS.executeUpdate();
        pS.close();

        pS = conn.prepareStatement("CREATE TABLE foo ( id INTEGER, name VARCHAR(100) )");
        pS.executeUpdate();
        pS.close();
    } catch (SQLException e) {
        throw new RuntimeException(e);
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:com.alibaba.napoli.metamorphosis.client.consumer.storage.JDBCUtils.java

public static Connection getConnection(DataSource ds) {
    try {//w w w  . ja v a  2  s .c  o  m
        return ds.getConnection();
    } catch (SQLException e) {
        throw new CannotGetConnectionException("Can not get connection from datasource", e);
    }
}

From source file:com.aaasec.sigserv.cscommon.SqLiteConnectionPool.java

/**
 * Gets a database connection from the connection pool for the provided connection URL.
 * If the connection URL is called for the first time, a new database connection pool
 * is created, else the connection pool created for that connection URL is used.
 * @param dbUrl The database source URL// w ww .ja v a 2 s. c o  m
 * @return Database connection
 */
public static Connection getConnection(String dbUrl, String userName, String password) throws SQLException {
    try {
        Class.forName("org.sqlite.JDBC");
    } catch (ClassNotFoundException ex) {
        LOG.log(Level.SEVERE, null, ex);
    }
    DataSource dataSource = getDataSource(dbUrl, userName, password);
    Connection conn = null;
    conn = dataSource.getConnection();
    return conn;
}

From source file:com.khartec.waltz.jobs.InvolvementHarness.java

private static void viaJdbc(DataSource dataSource) {

    try (Connection conn = dataSource.getConnection();) {

        System.out.println("-- jdbc start");
        long start = System.currentTimeMillis();

        PreparedStatement pstmt = conn.prepareStatement(qry);
        ResultSet rs = pstmt.executeQuery();

        int c = 0;

        while (rs.next()) {
            c++;/*  w  w  w. j a  v a2 s .c om*/
        }

        System.out.println(c);
        long duration = System.currentTimeMillis() - start;
        System.out.println("-- jdbc end " + duration);
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

From source file:br.gov.jfrj.siga.persistencia.oracle.JDBCUtilOracle.java

public static Connection getConnectionPool(final String dataSource) {

    Context initContext = null;/*from   w ww  . jav a  2  s  .co m*/
    Context envContext = null;
    Connection conn = null;

    try {
        JDBCUtilOracle.log.debug("Criando variavel de contexto");
        initContext = new InitialContext();

        envContext = (Context) initContext.lookup("java:/comp/env");
        JDBCUtilOracle.log.debug("Criando datasource ");
        final DataSource ds = (DataSource) envContext.lookup(dataSource);
        conn = ds.getConnection();

    } catch (final NamingException e) {
        JDBCUtilOracle.log.error(Messages.getString("Oracle.LookUpErro"));
        System.err.print(Messages.getString("Oracle.LookUpErro") + e.getMessage());

    } catch (final SQLException ex) {
        System.err.print(Messages.getString("Oracle.SQLErro") + ex.getMessage());
    }
    return conn;
}

From source file:com.googlecode.flyway.core.util.jdbc.JdbcUtils.java

/**
 * Opens a new connection from this dataSource.
 *
 * @param dataSource The dataSource to obtain the connection from.
 * @return The new connection.//from  w w  w.j  a  v  a 2 s  . c  o  m
 * @throws FlywayException when the connection could not be opened.
 */
public static Connection openConnection(DataSource dataSource) throws FlywayException {
    try {
        Connection connection = dataSource.getConnection();
        if (connection == null) {
            throw new FlywayException("Unable to obtain Jdbc connection from DataSource");
        }
        return connection;
    } catch (SQLException e) {
        throw new FlywayException("Unable to obtain Jdbc connection from DataSource", e);
    }
}