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.magnet.mmx.util.DatabaseExport.java

public static void main(String[] args) throws Exception {
    InputStream inputStream = DeviceDAOImplTest.class.getResourceAsStream("/test.properties");

    Properties testProperties = new Properties();
    testProperties.load(inputStream);/* www  .j  a  va 2 s.  com*/

    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;

    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName(driver);
    ds.setUsername(user);
    ds.setPassword(password);
    ds.setUrl(url);
    TreeMap<String, String> map = new TreeMap<String, String>();
    map.put("mmxTag", "SELECT * FROM mmxTag ");
    doWork(ds.getConnection(), map);
}

From source file:com.sinotopia.mybatis.plus.test.GlobalConfigurationTest.java

/**
 * ?/*from   w  w w  .  ja  v a 2 s .com*/
 */
@SuppressWarnings("unchecked")
public static void main(String[] args) {
    GlobalConfiguration global = GlobalConfiguration.defaults();
    global.setAutoSetDbType(true);
    // FieldStrategy.Empty
    global.setFieldStrategy(2);
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/mybatis-plus?characterEncoding=UTF-8");
    dataSource.setUsername("root");
    dataSource.setPassword("521");
    dataSource.setMaxTotal(1000);
    GlobalConfiguration.setMetaData(dataSource, global);
    // ?
    InputStream inputStream = GlobalConfigurationTest.class.getClassLoader()
            .getResourceAsStream("mysql-config.xml");
    MybatisSessionFactoryBuilder factoryBuilder = new MybatisSessionFactoryBuilder();
    factoryBuilder.setGlobalConfig(global);
    SqlSessionFactory sessionFactory = factoryBuilder.build(inputStream);
    SqlSession session = sessionFactory.openSession(false);
    TestMapper testMapper = session.getMapper(TestMapper.class);
    /*Wrapper type = Condition.instance().eq("id",1).or().in("type", new Object[]{1, 2, 3, 4, 5, 6});
    List list = testMapper.selectList(type);
    System.out.println(list.toString());*/
    Test test = new Test();
    test.setCreateTime(new Date());
    // ?
    test.setType("");
    testMapper.insert(test);

    SqlSession sqlSession = sessionFactory.openSession(false);
    NotPKMapper pkMapper = sqlSession.getMapper(NotPKMapper.class);
    NotPK notPK = new NotPK();
    notPK.setUuid(UUID.randomUUID().toString());
    int num = pkMapper.insert(notPK);
    Assert.assertTrue(num > 0);
    NotPK notPK1 = pkMapper.selectOne(notPK);
    Assert.assertNotNull(notPK1);
    pkMapper.selectPage(RowBounds.DEFAULT, Condition.create().eq("type", 12121212));
    NotPK notPK2 = null;
    try {
        notPK2 = pkMapper.selectById("1");
    } catch (Exception e) {
        System.out.println(",");
    }
    Assert.assertNull(notPK2);
    int count = pkMapper.selectCount(Condition.EMPTY);
    Assert.assertTrue(count > 0);
    int deleteCount = pkMapper.delete(null);
    Assert.assertTrue(deleteCount > 0);
    session.rollback();
    sqlSession.commit();
}

From source file:br.com.ceosites.cachedproperties.cache.test.util.DatabaseUtils.java

public static DataSource prepareDatabase() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setUrl("jdbc:hsqldb:mem:dataSource?hsqldb.sqllog=3");
    dataSource.setUsername("SA");
    dataSource.setPassword("");
    dataSource.setDriverClassName("org.hsqldb.jdbcDriver");
    ScriptRunner scriptRunner = new ScriptRunner(dataSource);

    try {//ww w .  j a  va2s  . c o m
        scriptRunner.execute(new BufferedReader(new FileReader("src/main/resources/db/create-db.sql")));
        scriptRunner.execute(new BufferedReader(new FileReader("src/main/resources/db/insert-data.sql")));
    } catch (IOException ex) {
        LOGGER.error("Error on read SQL files.", ex);
    } catch (SQLException ex) {
        LOGGER.error("Error on  execute SQL script.", ex);
    }

    return dataSource;
}

From source file:agency.Agency.java

private static DataSource prepareDataSource() throws SQLException, IOException {
    Properties myconf = new Properties();
    myconf.load(Agency.class.getResourceAsStream("/myconf.properties"));

    BasicDataSource ds = new BasicDataSource();

    ds.setUrl(myconf.getProperty("jdbc.url"));
    ds.setUsername(myconf.getProperty("jdbc.user"));
    ds.setPassword(myconf.getProperty("jdbc.password"));

    return ds;//  ww  w  . j  av a2 s .  c o m
}

From source file:cz.muni.fi.pv168.project.hotelmanager.Main.java

public static DataSource createMemoryDatabase() {
    log.info("starts creation of tables");
    BasicDataSource bds = new BasicDataSource();
    //set JDBC driver and URL
    bds.setDriverClassName(EmbeddedDriver.class.getName());
    log.info("try create Datasource");
    bds.setUrl("jdbc:derby:memory:HotelManagerDB;create=true");
    //populate db with tables and data
    log.info("try create tables");
    new ResourceDatabasePopulator(new ClassPathResource("schema-javadb.sql")).execute(bds);
    log.info("return datasource");
    return bds;/*w w w . jav  a2s  .  c  o m*/
}

From source file:gruposinvestigacion.model.util.Pool.java

private static void inicializarDataSource() {
    BasicDataSource dsBasico = new BasicDataSource();
    dsBasico.setDriverClassName(db_driver);
    dsBasico.setUsername(db_usuario);/*from  w  w  w.j a  v a  2 s  .c  om*/
    dsBasico.setPassword(db_contrasena);
    dsBasico.setUrl(db_url);
    dsBasico.setMaxTotal(100);
    datasource = dsBasico;
}

From source file:com.edu.ufps.maregroups.util.Pool.java

private static void inicializarDataSource() {
    BasicDataSource dsBasico = new BasicDataSource();
    dsBasico.setDriverClassName(DB_DRIVER);
    dsBasico.setUsername(DB_USUARIO);//  w ww.j a  va 2  s.co m
    dsBasico.setPassword(DB_CONTRASENA);
    dsBasico.setUrl(DB_URL);
    dsBasico.setMaxTotal(100);
    datasource = dsBasico;
}

From source file:com.github.akiraly.db4j.pool.DbcpUtils.java

public static BasicDataSource newDefaultDS() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDefaultAutoCommit(false);

    dataSource.setDefaultQueryTimeout(1);
    dataSource.setValidationQueryTimeout(1);
    dataSource.setMaxWaitMillis(5000);//from   ww  w .ja  v a  2s .  c o m

    dataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);

    dataSource.setInitialSize(4);
    dataSource.setMinIdle(4);
    dataSource.setMaxIdle(8);
    dataSource.setMaxTotal(16);
    dataSource.setPoolPreparedStatements(true);
    dataSource.setMaxOpenPreparedStatements(128);
    return dataSource;
}

From source file:com.fluke.database.DatabaseProperty.java

public static DataSource getDataSource() {
    if (source != null) {
        return source;
    }//w ww. j  a  v  a  2  s  .c  o  m
    Properties props = new Properties();
    FileInputStream fis = null;

    BasicDataSource dataSource = new BasicDataSource();
    try {

        props.load(ClassLoader.getSystemResourceAsStream("config/mysql.properties"));
        dataSource.setDriverClassName(props.getProperty("MYSQL_DB_DRIVER_CLASS"));
        dataSource.setUrl(props.getProperty("MYSQL_DB_URL"));
        dataSource.setUsername(props.getProperty("MYSQL_DB_USERNAME"));
        dataSource.setPassword(props.getProperty("MYSQL_DB_PASSWORD"));

    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return source = dataSource;
}

From source file:com.rhino.data.db.DataSourceFactory.java

public static DataSource getDataSource() {
    if (source != null) {
        return source;
    }//from  w w w . j a va 2s .  c om
    Properties props = new Properties();
    FileInputStream fis = null;

    BasicDataSource dataSource = new BasicDataSource();
    try {

        props.load(ClassLoader.getSystemResourceAsStream("mysql.properties"));
        dataSource.setDriverClassName(props.getProperty("MYSQL_DB_DRIVER_CLASS"));
        dataSource.setUrl(props.getProperty("MYSQL_DB_URL"));
        dataSource.setUsername(props.getProperty("MYSQL_DB_USERNAME"));
        dataSource.setPassword(props.getProperty("MYSQL_DB_PASSWORD"));

    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return source = dataSource;
}