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

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

Introduction

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

Prototype

BasicDataSource

Source Link

Usage

From source file:net.daw.connection.implementation.DbcpConnectionPoolImpl.java

@Override
public Connection newConnection() throws Exception {
    Connection c = null;/*ww w  . ja  v a 2s  .com*/
    basicDataSource = new BasicDataSource();
    basicDataSource.setDriverClassName("com.mysql.jdbc.Driver");
    basicDataSource.setUsername("root");
    basicDataSource.setPassword("bitnami");
    basicDataSource.setUrl("jdbc:mysql://127.0.0.1:3306/ausiaxContent");
    basicDataSource.setValidationQuery("select 1");
    basicDataSource.setMaxActive(100);
    basicDataSource.setMaxWait(10000);
    basicDataSource.setMaxIdle(10);

    return c;
}

From source file:com.talkingdata.orm.tool.ORMGenerateAction.java

@Override
public void actionPerformed(AnActionEvent e) {
    Project project = e.getRequiredData(CommonDataKeys.PROJECT);
    ORMConfig config = ORMConfig.getInstance(project);

    // 1. validate input parameter
    if (!validateConfig(project, config)) {
        return;//from www. jav a2 s . c  o m
    }

    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource.setUrl(
            String.format("jdbc:mysql://%s:%s/%s", config.getIp(), config.getPort(), config.getDatabase()));
    dataSource.setUsername(config.getUser());
    dataSource.setPassword(config.getPassword());

    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

    // 2. validate database is connected
    try {
        jdbcTemplate.execute("SELECT 1");
    } catch (Exception exception) {
        Messages.showWarningDialog(project, "The database is not connected.", "Warning");
        return;
    }

    File resourceDirectory = new File(project.getBasePath(), "/src/main/resources/mybatis");
    if (!resourceDirectory.exists()) {
        resourceDirectory.mkdirs();
    }

    File packageDirectory = new File(project.getBasePath(),
            "/src/main/java/" + config.getPackageName().replaceAll("\\.", "/"));
    if (!packageDirectory.exists()) {
        packageDirectory.mkdirs();
    }

    Properties p = new Properties();
    p.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS,
            "org.apache.velocity.runtime.log.Log4JLogChute");
    Velocity.init(p);

    // 3. query all table
    SqlParser sqlParser = new SqlParser();

    try {
        for (ClassDefinition cls : sqlParser.getTables(jdbcTemplate, config.getDatabase(),
                config.getPackageName())) {
            Map<String, Object> map = new HashMap<>(1);
            map.put("cls", cls);

            File domainDirectory = new File(packageDirectory, "domain");
            if (!domainDirectory.exists()) {
                domainDirectory.mkdirs();
            }
            File clsFile = new File(domainDirectory, cls.getClassName() + ".java");
            if (!clsFile.exists()) {
                clsFile.createNewFile();
            }
            writeFile("com/talkingdata/orm/tool/vm/class.vm", map, new FileWriter(clsFile));

            File daoDirectory = new File(packageDirectory, "dao");
            if (!daoDirectory.exists()) {
                daoDirectory.mkdirs();
            }
            File daoFile = new File(daoDirectory, cls.getClassName() + "Dao.java");
            if (!daoFile.exists()) {
                daoFile.createNewFile();
            }
            writeFile("com/talkingdata/orm/tool/vm/dao.vm", map, new FileWriter(daoFile));

            File mapperFile = new File(resourceDirectory, cls.getClassInstanceName() + ".xml");
            if (!mapperFile.exists()) {
                mapperFile.createNewFile();
            }
            writeFile("com/talkingdata/orm/tool/vm/mapper.vm", map, new FileWriter(mapperFile));
        }
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }

}

From source file:gov.nih.nci.cacis.xds.authz.config.JpaConfig.java

/**
* Will return the Data source.//from   w  w  w .j  ava2s  . c  om
*
* @return the data source
*/
@Bean(destroyMethod = "close")
public DataSource dataSource() {
    final BasicDataSource dataSource = new BasicDataSource();
    dataSource.setUrl(url);
    dataSource.setPassword(password);
    dataSource.setUsername(username);
    dataSource.setDriverClassName(driverClassName);

    return dataSource;
}

From source file:com.dangdang.ddframe.job.event.rdb.JobEventRdbListenerTest.java

@Before
public void setUp() throws JobEventListenerConfigurationException, SQLException, NoSuchFieldException {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName(org.h2.Driver.class.getName());
    dataSource.setUrl("jdbc:h2:mem:job_event_storage");
    dataSource.setUsername("sa");
    dataSource.setPassword("");
    JobEventRdbListener jobEventRdbListener = new JobEventRdbListener(dataSource);
    ReflectionUtils.setFieldValue(jobEventRdbListener, "repository", repository);
    when(jobEventRdbConfiguration.createJobEventListener()).thenReturn(jobEventRdbListener);
    jobEventBus = new JobEventBus(jobEventRdbConfiguration);
}

From source file:com.futuretech.config.AppConfig.java

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

    return dataSource;
}

From source file:com.gnizr.core.web.junit.GnizrWebappTestBase.java

private void init() {
    if (dataSource == null) {
        dataSource = new BasicDataSource();
        dataSource.setUsername("gnizr");
        dataSource.setPassword("gnizr");
        dataSource.setUrl("jdbc:mysql://localhost/gnizr_test");
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    }/* www  .  j  av a 2  s  .c  om*/

    gnizrDBDao = new GnizrDao();
    gnizrDBDao.setBookmarkDao(new BookmarkDBDao(dataSource));
    gnizrDBDao.setUserDao(new UserDBDao(dataSource));
    gnizrDBDao.setLinkDao(new LinkDBDao(dataSource));
    gnizrDBDao.setTagDao(new TagDBDao(dataSource));
    gnizrDBDao.setTagPropertyDao(new TagPropertyDBDao(dataSource));
    gnizrDBDao.setTagAssertionDao(new TagAssertionDBDao(dataSource));
    gnizrDBDao.setFeedSubscriptionDao(new FeedSubscriptionDBDao(dataSource));
    gnizrDBDao.setForUserDao(new ForUserDBDao(dataSource));
    gnizrDBDao.setFolderDao(new FolderDBDao(dataSource));
    gnizrDBDao.setGeometryMarkerDao(new GeometryMarkerDBDao(dataSource));
}

From source file:example.unittest.ExampleDbUnitTest.java

@Before
public void setup() {
    DbEnvironment db = new UnitTestDbBuilder().setSourcePath("./src/main/database").setReferenceEnvName("test")
            .setDbPlatform(new HsqlDbPlatform()).setDbServer("mytest").buildContext().setupAndCleanAndDeploy()
            .getEnvironment();//  ww w.  ja v  a 2 s  .  c  o  m

    System.out.println("in case you want to see what the url will be: " + db.getJdbcUrl());

    // create the datasource for your test. You have the option to append URL parameters to simulate your actual app conditions if you'd like
    BasicDataSource ds = new BasicDataSource();
    ds.setUrl(db.getJdbcUrl() + ";schema=DEMO_SCHEMA");
    ds.setUsername(db.getDefaultUserId());
    ds.setPassword(db.getDefaultPassword());
    this.ds = ds;
}

From source file:com.infodev.fcgorole.configuration.AppConfig.java

@Bean(name = "dataSource")
public BasicDataSource dataSource() {

    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName("com.mysql.jdbc.Driver");
    ds.setUrl("jdbc:mysql://localhost:3306/fcgorecondb");
    ds.setUsername("root");
    return ds;/*  w w w  .ja  v  a 2  s .c  o m*/
}

From source file:com.ibatis.common.jdbc.DbcpConfiguration.java

private BasicDataSource newDbcpConfiguration(Map map) {
    BasicDataSource basicDataSource = new BasicDataSource();
    Iterator props = map.keySet().iterator();
    while (props.hasNext()) {
        String propertyName = (String) props.next();
        if (propertyName.startsWith(ADD_DRIVER_PROPS_PREFIX)) {
            String value = (String) map.get(propertyName);
            basicDataSource.addConnectionProperty(propertyName.substring(ADD_DRIVER_PROPS_PREFIX_LENGTH),
                    value);//from  w  w  w . j  av  a 2  s .  c  om
        } else if (PROBE.hasWritableProperty(basicDataSource, propertyName)) {
            String value = (String) map.get(propertyName);
            Object convertedValue = convertValue(basicDataSource, propertyName, value);
            PROBE.setObject(basicDataSource, propertyName, convertedValue);
        }
    }
    return basicDataSource;
}

From source file:gobblin.util.jdbc.DataSourceProvider.java

@Inject
public DataSourceProvider(@Named("dataSourceProperties") Properties properties) {
    this.basicDataSource = new BasicDataSource();
    this.basicDataSource.setDriverClassName(properties.getProperty(CONN_DRIVER, DEFAULT_CONN_DRIVER));
    this.basicDataSource.setUrl(properties.getProperty(CONN_URL));
    if (properties.containsKey(USERNAME) && properties.containsKey(PASSWORD)) {
        this.basicDataSource.setUsername(properties.getProperty(USERNAME));
        this.basicDataSource.setPassword(
                PasswordManager.getInstance(properties).readPassword(properties.getProperty(PASSWORD)));
    }//from   w ww .ja v a 2s . co m
    if (properties.containsKey(MAX_IDLE_CONNS)) {
        this.basicDataSource.setMaxIdle(Integer.parseInt(properties.getProperty(MAX_IDLE_CONNS)));
    }
    if (properties.containsKey(MAX_ACTIVE_CONNS)) {
        this.basicDataSource.setMaxActive(Integer.parseInt(properties.getProperty(MAX_ACTIVE_CONNS)));
    }
}