Example usage for org.apache.commons.dbcp2 BasicDataSource BasicDataSource

List of usage examples for org.apache.commons.dbcp2 BasicDataSource BasicDataSource

Introduction

In this page you can find the example usage for org.apache.commons.dbcp2 BasicDataSource BasicDataSource.

Prototype

BasicDataSource

Source Link

Usage

From source file:com.alehuo.wepas2016projekti.configuration.ProductionConfiguration.java

/**
 * Heroku -palvelussa kytetn PostgreSQL -tietokantaa tiedon
 * tallentamiseen. Tm metodi hakee herokusta tietokantayhteyden
 * mahdollistavat ympristmuuttujat.// w  w w. j  a  va 2s  . c o  m
 *
 * @return @throws URISyntaxException
 */
@Bean
public BasicDataSource dataSource() throws URISyntaxException {
    URI dbUri = new URI(System.getenv("DATABASE_URL"));

    String username = dbUri.getUserInfo().split(":")[0];
    String password = dbUri.getUserInfo().split(":")[1];
    String dbUrl = "jdbc:postgresql://" + dbUri.getHost() + ':' + dbUri.getPort() + dbUri.getPath();

    BasicDataSource basicDataSource = new BasicDataSource();
    basicDataSource.setUrl(dbUrl);
    basicDataSource.setUsername(username);
    basicDataSource.setPassword(password);

    //Autocommit pois plt
    basicDataSource.setDefaultAutoCommit(false);

    return basicDataSource;
}

From source file:com.library.bookarticlelibrary.WebConfig.java

@Bean(name = "usersdb")
public DataSource getDataSource() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql://localhost:3306/usersdb");
    dataSource.setUsername("root");
    dataSource.setPassword("Welkom01");

    return dataSource;
}

From source file:com.bc.fiduceo.TestUtil.java

public static BasicDataSource getDatasource_H2() {
    final BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName("org.h2.Driver");
    // the following line dumps all database interactions to the console window tb 2016-02-10
    //        dataSource.setUrl("jdbc:h2:mem:fiduceo;TRACE_LEVEL_SYSTEM_OUT=2");
    dataSource.setUrl("jdbc:h2:mem:fiduceo");
    dataSource.setUsername("sa");
    dataSource.setPassword("");
    return dataSource;
}

From source file:BasicDataSourceExample.java

public static DataSource setupDataSource(String connectURI) {
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName("org.h2.Driver");
    ds.setUrl(connectURI);//from   w w w  .  j  a v a2  s . com
    return ds;
}

From source file:com.qwazr.connectors.DatabaseConnector.java

@Override
public void load(File data_directory) {
    try {/* w  w  w.j a  va2 s  . c  o m*/
        if (pool == null) {
            JDBCConnection cnx = new JDBCConnection();
            if (!StringUtils.isEmpty(driver))
                cnx.setDriver(ClassLoaderManager.classLoader, driver);
            if (!StringUtils.isEmpty(url))
                cnx.setUrl(url);
            if (!StringUtils.isEmpty(username))
                cnx.setUsername(username);
            if (!StringUtils.isEmpty(password))
                cnx.setPassword(password);
            connectionManager = cnx;
        } else {
            basicDataSource = new BasicDataSource();
            if (driver != null)
                basicDataSource.setDriverClassName(driver);
            if (url != null)
                basicDataSource.setUrl(url);
            if (username != null)
                basicDataSource.setUsername(username);
            if (password != null)
                basicDataSource.setPassword(password);
            if (pool.initial_size != null)
                basicDataSource.setInitialSize(pool.initial_size);
            if (pool.min_idle != null)
                basicDataSource.setMinIdle(pool.min_idle);
            if (pool.max_idle != null)
                basicDataSource.setMaxIdle(pool.max_idle);
            if (pool.max_total != null)
                basicDataSource.setMaxTotal(pool.max_total);
            if (pool.max_wait_millis != null)
                basicDataSource.setMaxWaitMillis(pool.max_wait_millis);
            if (pool.log_abandoned != null)
                basicDataSource.setLogAbandoned(pool.log_abandoned);
            if (pool.log_expired_connections != null)
                basicDataSource.setLogExpiredConnections(pool.log_expired_connections);
            if (pool.abandoned_usage_tracking != null)
                basicDataSource.setAbandonedUsageTracking(pool.abandoned_usage_tracking);
            connectionManager = new DataSourceConnection(basicDataSource);

        }
    } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
        logger.error(e.getMessage(), e);
        throw new RuntimeException(e);
    }
}

From source file:com.jk.db.datasource.JKPoolingDataSource.java

@Override
protected void init() {
    super.init();
    this.logger.debug("Constructing database pool with settings :", JKCollectionUtil.toString(getProperties()));
    this.datasource = new BasicDataSource();
    this.datasource.setDriverClassName(super.getDriverName());
    this.datasource.setUrl(getDatabaseUrl());
    this.datasource.setUsername(getUsername());
    this.datasource.setPassword(getPassword());
    this.datasource.setInitialSize(getInitialPoolSize());
    this.datasource.setMaxTotal(getMaxPoolSize());
}

From source file:com.javacreed.api.secureproperties.examples.example1.Example.java

private void connect() {
    dataSource = new BasicDataSource();
    dataSource.setDriverClassName("org.h2.Driver");
    dataSource.setUrl("jdbc:h2:./target/test");
    dataSource.setUsername("sa");
    dataSource.setPassword("");
}

From source file:com.gondor.config.ApplicationContextConfig.java

@Bean(name = "dataSource")
public DataSource getDataSource() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql://localhost:3306/usersdb");
    dataSource.setUsername("root");
    dataSource.setPassword("root");
    return dataSource;
}

From source file:moviemanager.backend.SpringConfig.java

@Bean
public DataSource dataSource() {
    /*BasicDataSource bds = new BasicDataSource(); //Apache DBCP connection pooling DataSource
    bds.setDriverClassName(env.getProperty("jdbc.driver"));
    bds.setUrl(env.getProperty("jdbc.url"));
    bds.setUsername(env.getProperty("jdbc.user"));
    bds.setPassword(env.getProperty("jdbc.password"));
    return bds;*//*from  w w  w.ja va  2  s.co m*/
    /*Properties conf = new Properties();
    BasicDataSource ds = new BasicDataSource();
    try {            
        conf.load(SpringConfig.class.getResourceAsStream("/myconf.properties"));            
    } catch (IOException ex) {
    //log.error("Loading properties has failed!");
    }
    try {
        DriverManager.getConnection(conf.getProperty("jdbc.url"));
            
    } catch (SQLException ex) {
        try {
            Connection conn = DriverManager.getConnection(conf.getProperty("jdbc.url"), conf.getProperty("jdbc.user"), conf.getProperty("jdbc.password"));
            //Connection conn = DriverManager.getConnection("org.apache.jdbc.ClientDriver" + "create=true;", "administrator", "admin");
        } catch (SQLException ex1) {
            Logger.getLogger(SpringConfig.class.getName()).log(Level.SEVERE, null, ex1);
        }
    }
            
    ds.setUrl(conf.getProperty("jdbc.url"));      
            
    return ds;*/
    Properties conf = new Properties();
    BasicDataSource ds = new BasicDataSource();
    try {
        conf.load(SpringConfig.class.getResourceAsStream("/myconf.properties"));
    } catch (IOException ex) {
        //log.error("Loading properties has failed!");
    }
    ds.setUrl(conf.getProperty("jdbc.url"));
    ds.setDriverClassName(conf.getProperty("jdbc.driver"));
    ds.setUsername(conf.getProperty("jdbc.user"));
    ds.setPassword(conf.getProperty("jdbc.password"));

    return ds;
    /*
    BasicDataSource bds = new BasicDataSource();
    bds.setDriverClassName("org.apache.derby.jdbc.ClientDriver");
    bds.setUrl("jdbc:derby://localhost:1527/MovieManagerDtb");
    bds.setUsername("administrator");
    bds.setPassword("admin");
    return bds;*/

    /*return new EmbeddedDatabaseBuilder()
        .setType(DERBY)
        .build();*/
}

From source file:com.magnet.mmx.server.plugin.mmxmgmt.api.tags.MMXTopicTagsResourceTest.java

@BeforeClass
public static void setupDatabase() throws Exception {
    java.util.logging.Logger.getLogger("com.google.inject").setLevel(java.util.logging.Level.SEVERE);
    org.apache.log4j.Logger.getLogger("org.apache.http").setLevel(org.apache.log4j.Level.DEBUG);
    org.apache.log4j.Logger.getLogger("org.jboss.resteasy").setLevel(Level.OFF);
    InputStream inputStream = MMXTopicTagsResourceTest.class.getResourceAsStream("/test.properties");

    Properties testProperties = new Properties();
    testProperties.load(inputStream);//  w  w w .  ja v a 2 s. c  om

    String host = testProperties.getProperty("db.host");
    String port = testProperties.getProperty("db.port");
    String user = testProperties.getProperty("db.user");
    String password = testProperties.getProperty("db.password");
    String driver = testProperties.getProperty("db.driver");
    String schema = testProperties.getProperty("db.schema");

    String url = "jdbc:mysql://" + host + ":" + port + "/" + schema;

    ds = new BasicDataSource();
    ds.setDriverClassName(driver);
    ds.setUsername(user);
    ds.setPassword(password);
    ds.setUrl(url);

    DBTestUtil.setBasicDataSource(ds);
    new MockUp<AppDAOImpl>() {
        @Mock
        protected String getEncrypted(String value) {
            return value;
        }
    };
    DBTestUtil.setupMockDBUtil();
    appEntity = createRandomApp();
    for (int i = 0; i < 5; i++) {
        appTopicEntityList.add(createRandomTopicEntity(appEntity, null, i));
    }
    for (int i = 0; i < 5; i++) {
        personalTopicEntityList.add(createRandomTopicEntity(appEntity, "user" + i, i));
    }
    LOGGER.warn("Finished setupDatabase");
}