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(String username, String password) throws SQLException;

Source Link

Document

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

Usage

From source file:com.micromux.cassandra.jdbc.DataSourceTest.java

@Test
public void testConstructor() throws Exception {
    CassandraDataSource cds = new CassandraDataSource(HOST, PORT, KEYSPACE, USER, PASSWORD, VERSION,
            CONSISTENCY, TRUST_STORE, TRUST_PASS);
    assertEquals(HOST, cds.getServerName());
    assertEquals(PORT, cds.getPortNumber());
    assertEquals(KEYSPACE, cds.getDatabaseName());
    assertEquals(USER, cds.getUser());/*  ww w .j  a  v a 2 s  . c  om*/
    assertEquals(PASSWORD, cds.getPassword());
    assertEquals(VERSION, cds.getVersion());

    DataSource ds = new CassandraDataSource(HOST, PORT, KEYSPACE, USER, PASSWORD, VERSION, CONSISTENCY,
            TRUST_STORE, TRUST_PASS);
    assertNotNull(ds);

    // null username and password
    java.sql.Connection cnx = ds.getConnection(null, null);
    assertFalse(cnx.isClosed());
    ds.setLoginTimeout(5);
    assertEquals(5, ds.getLoginTimeout());

    // no username and password
    cnx = ds.getConnection();
    assertFalse(cnx.isClosed());
    ds.setLoginTimeout(5);
    assertEquals(VERSION, ((CassandraConnection) cnx).getConnectionProps().get(Utils.TAG_CQL_VERSION));
    assertEquals(5, ds.getLoginTimeout());
}

From source file:nl.nn.adapterframework.jdbc.JdbcFacade.java

/**
 * Obtains a connection to the datasource. 
 *//*from  w  w w .j a  v a 2 s  .  co  m*/
// TODO: consider making this one protected.
public Connection getConnection() throws JdbcException {
    DataSource ds = getDatasource();
    try {
        if (StringUtils.isNotEmpty(getUsername())) {
            return ds.getConnection(getUsername(), getPassword());
        }
        return ds.getConnection();
    } catch (SQLException e) {
        throw new JdbcException(
                getLogPrefix() + "cannot open connection on datasource [" + getDataSourceNameToUse() + "]", e);
    }
}

From source file:org.apache.ojb.broker.metadata.JdbcMetadataUtils.java

/**
 * Fills parameters of the given {@link JdbcConnectionDescriptor} with metadata
 * extracted from the given datasource./*from  ww w .ja v  a  2  s.c o  m*/
 * 
 * @param jcd        The jdbc connection descriptor to fill
 * @param dataSource The data source
 * @param username   The username required to establish a connection via the data source
 *                   Can be empty if the data source does not require it or if one
 *                   is specified in the jdbc connection descriptor
 * @param password   The username required to establish a connection via the data source
 *                   Can be empty if the data source or username does not require it or if one
 *                   is specified in the jdbc connection descriptor
 */
public void fillJCDFromDataSource(JdbcConnectionDescriptor jcd, DataSource dataSource, String username,
        String password) throws MetadataException {
    String realUsername = (jcd.getUserName() != null ? jcd.getUserName() : username);
    String realPassword = (jcd.getPassWord() != null ? jcd.getPassWord() : password);
    Connection connection = null;
    DatabaseMetaData metadata = null;

    try {
        // we have to open a connection to be able to retrieve metadata
        if (realUsername != null) {
            connection = dataSource.getConnection(realUsername, realPassword);
        } else {
            connection = dataSource.getConnection();
        }

        metadata = connection.getMetaData();
    } catch (Throwable t) {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException ex) {
            }
        }
        throw new MetadataException("Could not get the metadata from the given datasource", t);
    }

    try {
        HashMap urlComponents = parseConnectionUrl(metadata.getURL());

        if (urlComponents.containsKey(PROPERTY_DBALIAS)) {
            jcd.setProtocol((String) urlComponents.get(PROPERTY_PROTOCOL));
            jcd.setSubProtocol((String) urlComponents.get(PROPERTY_SUBPROTOCOL));
            jcd.setDbAlias((String) urlComponents.get(PROPERTY_DBALIAS));
            if (jdbcSubProtocolToPlatform.containsKey(jcd.getSubProtocol())) {
                // TODO: We might be able to use this: metadata.getDatabaseProductName();
                jcd.setDbms((String) jdbcSubProtocolToPlatform.get(jcd.getSubProtocol()));
            }
        }
    } catch (Throwable t) {
        try {
            connection.close();
        } catch (SQLException ex) {
        }
        throw new MetadataException("Could not get the metadata from the given datasource", t);
    }
    try {
        // this will only work with JDK >= 1.4 and only with some jdbc drivers
        Integer majorVersion = (Integer) PropertyUtils.getProperty(metadata, "JDBCMajorVersion");
        Integer minorVersion = (Integer) PropertyUtils.getProperty(metadata, "JDBCMinorVersion");

        jcd.setJdbcLevel(Double.parseDouble(majorVersion.toString() + "." + minorVersion.toString()));
    } catch (Throwable t) {
        // otherwise we're assuming JDBC 2.0 compliance
        jcd.setJdbcLevel(2.0);
    }
    try {
        connection.close();
    } catch (SQLException ex) {
    }
}

From source file:jdao.JDAO.java

public static Connection createConnectionByDataSourceSpec(String dsclazz, String jdbcUri, String userName,
        String password) {/*  w  w  w  . jav a2 s . c o m*/
    try {
        if (dsclazz != null && !dsclazz.equalsIgnoreCase("")) {
            DataSource ds = (DataSource) Class
                    .forName(dsclazz, true, Thread.currentThread().getContextClassLoader()).newInstance();
            BeanUtils.setProperty(ds, "url", jdbcUri);
            return ds.getConnection(userName, password);
        }
        return DriverManager.getConnection(jdbcUri, userName, password);
    } catch (Exception xe) {
        log("Error : ", xe);
        return null;
    }
}