Example usage for org.springframework.jdbc.datasource DriverManagerDataSource DriverManagerDataSource

List of usage examples for org.springframework.jdbc.datasource DriverManagerDataSource DriverManagerDataSource

Introduction

In this page you can find the example usage for org.springframework.jdbc.datasource DriverManagerDataSource DriverManagerDataSource.

Prototype

public DriverManagerDataSource() 

Source Link

Document

Constructor for bean-style configuration.

Usage

From source file:com.reactivetechnologies.analytics.BootstrapConfigurator.java

@ConfigurationProperties(prefix = "spring.datasource")
@Bean
public DataSource unpooled() {
    return new DriverManagerDataSource();
}

From source file:io.convergencia.training.Application.java

@Bean
public DataSource dataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName("org.postgresql.Driver");
    dataSource.setUrl("jdbc:postgresql://localhost:5432/training");
    dataSource.setUsername("postgres");
    dataSource.setPassword("123456");
    return dataSource;
}

From source file:net.firejack.platform.core.utils.db.DBUtils.java

/**
 * @param driverClassName driver classname
 * @param url jdbc url//from w ww .j a v  a  2  s.c o  m
 * @param user db user
 * @param password db user password
 * @return returns populated data-source
 */
public static DataSource populateDataSource(String driverClassName, String url, String user, String password) {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName(driverClassName);
    dataSource.setUrl(url);
    dataSource.setUsername(user);
    dataSource.setPassword(password);
    return dataSource;
}

From source file:com.baidu.rigel.biplatform.tesseract.datasource.impl.SqlDataSourceManagerImpl.java

/**
 * ?datasourceinfo ???//from   w w  w  . java  2  s  .c o  m
 * 
 * @param dataSourceInfo ???
 * @return ???
 */
private DynamicSqlDataSource createDynamicDataSource(SqlDataSourceInfo dataSourceInfo)
        throws DataSourceException {
    MetaDataService.checkDataSourceInfo(dataSourceInfo);
    Map<String, SqlDataSourceWrap> dataSources = new HashMap<String, SqlDataSourceWrap>();

    for (int i = 0; i < dataSourceInfo.getHosts().size(); i++) {

        String dataSourceKey = generateDataSourceKey(dataSourceInfo.getProductLine(),
                dataSourceInfo.getHosts().get(i), dataSourceInfo.getInstanceName(),
                dataSourceInfo.getUsername(), Md5Util.encode(dataSourceInfo.toString())).replace(":",
                        DATASOURCEKEY_SEPRATE);
        if (dataSourceInfo.isDBProxy()) {
            // dbproxy?
            DriverManagerDataSource dataSource = new DriverManagerDataSource();
            dataSource.setDriverClassName(dataSourceInfo.getDataBase().getDriver());
            dataSource.setUrl(dataSourceInfo.getJdbcUrls().get(i));
            dataSource.setUsername(dataSourceInfo.getUsername());
            try {
                dataSource.setPassword(dataSourceInfo.getPassword());
            } catch (Exception e) {
                log.error("set dataSource password error," + dataSourceInfo, e);
                throw new DataSourceException("set dataSource password error," + dataSourceInfo, e);
            }
            dataSources.put(dataSourceKey, new SqlDataSourceWrap(dataSource));
        } else {
            ComboPooledDataSource dataSource = new ComboPooledDataSource();
            dataSource.setDataSourceName(dataSourceKey);
            try {
                dataSource.setDriverClass(dataSourceInfo.getDataBase().getDriver());
            } catch (PropertyVetoException e) {
                log.error("set dataSource driverclass error," + dataSourceInfo, e);
                // ??
                throw new DataSourceException(
                        "set c3p0 driverclass error when create datasource with:" + dataSourceInfo, e);
            }
            dataSource.setJdbcUrl(dataSourceInfo.getJdbcUrls().get(i));
            dataSource.setUser(dataSourceInfo.getUsername());
            try {
                dataSource.setPassword(dataSourceInfo.getPassword());
            } catch (Exception e) {
                log.error("set dataSource password error," + dataSourceInfo, e);
                throw new DataSourceException("set dataSource password error," + dataSourceInfo, e);
            }

            dataSource.setInitialPoolSize(Integer.valueOf(dataSourceInfo.getConnectionProperties(
                    DataSourceInfo.JDBC_INITIALPOOLSIZE_KEY, DataSourceInfo.JDBC_INITIALPOOLSIZE)));
            dataSource.setMaxPoolSize(Integer.valueOf(dataSourceInfo.getConnectionProperties(
                    DataSourceInfo.JDBC_MAXPOOLSIZE_KEY, DataSourceInfo.JDBC_MAXPOOLSIZE)));
            dataSource.setMinPoolSize(Integer.valueOf(dataSourceInfo.getConnectionProperties(
                    DataSourceInfo.JDBC_MINPOOLSIZE_KEY, DataSourceInfo.JDBC_MINPOOLSIZE)));

            dataSource.setIdleConnectionTestPeriod(Integer.valueOf(
                    dataSourceInfo.getConnectionProperties(DataSourceInfo.JDBC_IDLECONNECTIONTESTPERIOD_KEY,
                            DataSourceInfo.JDBC_IDLECONNECTIONTESTPERIOD)));
            dataSource.setMaxIdleTime(Integer.valueOf(dataSourceInfo.getConnectionProperties(
                    DataSourceInfo.JDBC_MAXIDLETIME_KEY, DataSourceInfo.JDBC_MAXIDLETIME)));
            dataSource.setCheckoutTimeout(Integer.valueOf(dataSourceInfo.getConnectionProperties(
                    DataSourceInfo.JDBC_CHECKTIMEOUT_KEY, DataSourceInfo.JDBC_CHECKTIMEOUT)));
            log.info("add datasource info into c3p0 pool success:" + dataSourceInfo);

            dataSources.put(dataSourceKey, new SqlDataSourceWrap(dataSource));
        }

    }
    return new DynamicSqlDataSource(dataSources);
}

From source file:gov.nih.nci.security.util.ConfigurationHelper.java

private DataSource getDataSourceFromDocument(Document hibernateConfigDoc) throws CSConfigurationException {
    Element hbnConfigElement = hibernateConfigDoc.getRootElement();
    DataSource ds = null;/*w  w  w . ja v a  2s . c  om*/
    org.jdom.Element urlProperty = null, usernameProperty = null, passwordProperty = null,
            driverProperty = null;
    try {
        org.jdom.Element dataSourceProperty = (org.jdom.Element) XPath.selectSingleNode(hibernateConfigDoc,
                "/hibernate-configuration//session-factory//property[@name='connection.datasource']");
        if (dataSourceProperty != null && dataSourceProperty.getTextTrim() != null) {
            try {
                InitialContext initialContext = new InitialContext();
                ds = (DataSource) initialContext.lookup(dataSourceProperty.getTextTrim());
            } catch (NamingException ex) {
                ex.printStackTrace();
                throw new CSConfigurationException();
            }
        } else {
            urlProperty = (org.jdom.Element) XPath.selectSingleNode(hibernateConfigDoc,
                    "/hibernate-configuration//session-factory//property[@name='connection.url']");
            usernameProperty = (org.jdom.Element) XPath.selectSingleNode(hibernateConfigDoc,
                    "/hibernate-configuration//session-factory//property[@name='connection.username']");
            passwordProperty = (org.jdom.Element) XPath.selectSingleNode(hibernateConfigDoc,
                    "/hibernate-configuration//session-factory//property[@name='connection.password']");
            driverProperty = (org.jdom.Element) XPath.selectSingleNode(hibernateConfigDoc,
                    "/hibernate-configuration//session-factory//property[@name='connection.driver_class']");

            DriverManagerDataSource dataSource = new DriverManagerDataSource();
            dataSource.setDriverClassName(driverProperty.getTextTrim());
            dataSource.setUrl(urlProperty.getTextTrim());
            dataSource.setUsername(usernameProperty.getTextTrim());
            dataSource.setPassword(passwordProperty.getTextTrim());

            ds = dataSource;
        }
    } catch (JDOMException e) {
        e.printStackTrace();
        throw new CSConfigurationException();
    }
    return ds;
}

From source file:org.owasp.proxy.Main.java

private static DataSource createDataSource(Configuration config) throws SQLException {
    if (config.jdbcDriver == null)
        return null;
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName(config.jdbcDriver);
    dataSource.setUrl(config.jdbcUrl);/* ww  w.ja v a 2s.  c  o  m*/
    dataSource.setUsername(config.jdbcUser);
    dataSource.setPassword(config.jdbcPassword);
    return dataSource;
}

From source file:gov.nih.nci.ncicb.tcga.dcc.QCLiveTestDataGeneratorSlowTest.java

/**
 * Test the {@link QCLiveTestDataGenerator#setDccCommonDevJdbcTemplate(JdbcTemplate)} and 
 * {@link QCLiveTestDataGenerator#setDiseaseDevJdbcTemplate(JdbcTemplate)} methods with a {@link JdbcTemplate} instance
 * whose data source references a test account.
 * /* w w  w.  j a va  2 s .c o  m*/
 * <p>An {@link IllegalArgumentException} should be thrown indicating that test accounts are not allowed.
 */
@Test
public void testSetDevDataSourceWithTestAcct() {

    // Set up at data source and JDBC Connection template that uses a test account
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setUsername("test");
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

    // Try setting the COMMON PRODUCTION JdbcTemplate for the QCLiveTestDataGenerator, should throw an IllegalArgumentException
    QCLiveTestDataGenerator qcLiveTestDataGenerator = new QCLiveTestDataGenerator();
    try {
        qcLiveTestDataGenerator.setDccCommonDevJdbcTemplate(jdbcTemplate);
    } catch (IllegalArgumentException iae) {
        assertEquals(
                "Test account 'test' is not permitted for database connection property 'dccCommonDevJdbcTemplate'",
                iae.getMessage());
    }

    // Try setting the DISEASE PRODUCTION JdbcTemplate for the QCLiveTestDataGenerator, should throw an IllegalArgumentException
    try {
        qcLiveTestDataGenerator.setDiseaseDevJdbcTemplate(jdbcTemplate);
    } catch (IllegalArgumentException iae) {
        assertEquals(
                "Test account 'test' is not permitted for database connection property 'diseaseDevJdbcTemplate'",
                iae.getMessage());
    }
}

From source file:gov.nih.nci.ncicb.tcga.dcc.QCLiveTestDataGeneratorSlowTest.java

/**
 * Test the {@link QCLiveTestDataGenerator#setDccCommonLocalJdbcTemplate(JdbcTemplate)} and 
 * {@link QCLiveTestDataGenerator#setDiseaseLocalJdbcTemplate(JdbcTemplate)} methods with a {@link JdbcTemplate} instance
 * whose data source defines a URL that contains a hostname that points to a non-test database instance.
 * /*  w  w  w .ja v a2s  .  c  o m*/
 * <p>An {@link IllegalArgumentException} should be thrown indicating that non-test hostnames in the database URL are
 * not allowed.
 */
@Test
public void testSetInvalidDevJDBCURLForLocalDB() {

    // Set up at data source and JDBC Connection template that uses a test account
    String url = "invalidTestDBHostName";
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setUrl(url);
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

    // Try setting the COMMON LOCAL JdbcTemplate for the QCLiveTestDataGenerator, should throw an IllegalArgumentException
    QCLiveTestDataGenerator qcLiveTestDataGenerator = new QCLiveTestDataGenerator();
    try {
        qcLiveTestDataGenerator.setDccCommonLocalJdbcTemplate(jdbcTemplate);
    } catch (IllegalArgumentException iae) {
        assertEquals("Connection URL '" + url
                + "' for datasource defined by 'dccCommonLocalJdbcTemplate' does not point to a "
                + "known test database host. Expected one of [" + qcLiveTestDataGenerator.getTestDBHostNames()
                + "]", iae.getMessage());
    }

    // Try setting the DISEASE LOCAL JdbcTemplate for the QCLiveTestDataGenerator, should throw an IllegalArgumentException
    try {
        qcLiveTestDataGenerator.setDiseaseLocalJdbcTemplate(jdbcTemplate);
    } catch (IllegalArgumentException iae) {
        assertEquals("Connection URL '" + url
                + "' for datasource defined by 'diseaseLocalJdbcTemplate' does not point to a "
                + "known test database host. Expected one of [" + qcLiveTestDataGenerator.getTestDBHostNames()
                + "]", iae.getMessage());
    }
}

From source file:com.google.enterprise.connector.sharepoint.TestConfiguration.java

/**
 * gets a sample data source for user data store
 * //  w  w w .  ja  va 2  s . c  o m
 * @return
 */
public static DataSource getUserDataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName(TestConfiguration.driverClass);
    dataSource.setUrl(TestConfiguration.dbUrl);
    dataSource.setUsername(TestConfiguration.dbUsername);
    dataSource.setPassword(TestConfiguration.dbPassword);
    return dataSource;
}