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

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

Introduction

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

Prototype

public synchronized void setUrl(String url) 

Source Link

Document

Sets the #url .

Note: this method currently has no effect once the pool has been initialized.

Usage

From source file:net.comze.framework.orm.datasource.DbcpDataSourceFactory.java

private BasicDataSource initialize(Properties properties) {
    ObjectUtils.notNull(properties, "properties");
    BasicDataSource basicDataSource = new BasicDataSource();
    basicDataSource.setDriverClassName(properties.getProperty(JDBC_DRIVER));
    basicDataSource.setUrl(properties.getProperty(JDBC_URL));
    basicDataSource.setUsername(properties.getProperty(JDBC_USERNAME));
    basicDataSource.setPassword(properties.getProperty(JDBC_PASSWORD));

    if (properties.containsKey(DBCP_INITIALSIZE)) {
        basicDataSource.setInitialSize(Integer.parseInt(properties.getProperty(DBCP_INITIALSIZE)));
    }//from   w ww.  jav a 2  s  .c  o  m
    if (properties.containsKey(DBCP_MAXACTIVE)) {
        basicDataSource.setMaxActive(Integer.parseInt(properties.getProperty(DBCP_MAXACTIVE)));
    }
    if (properties.containsKey(DBCP_MAXIDLE)) {
        basicDataSource.setMaxIdle(Integer.parseInt(properties.getProperty(DBCP_MAXIDLE)));
    }
    if (properties.containsKey(DBCP_MAXWAIT)) {
        basicDataSource.setMaxWait(Long.parseLong(properties.getProperty(DBCP_MAXWAIT)));
    }
    if (properties.containsKey(DBCP_MINEVICTABLEIDLETIMEMILLIS)) {
        basicDataSource.setMinEvictableIdleTimeMillis(
                Long.parseLong(properties.getProperty(DBCP_MINEVICTABLEIDLETIMEMILLIS)));
    }
    if (properties.containsKey(DBCP_MINIDLE)) {
        basicDataSource.setMinIdle(Integer.parseInt(properties.getProperty(DBCP_MINIDLE)));
    }
    if (properties.containsKey(DBCP_NUMTESTSPEREVICTIONRUN)) {
        basicDataSource.setNumTestsPerEvictionRun(
                Integer.parseInt(properties.getProperty(DBCP_NUMTESTSPEREVICTIONRUN)));
    }
    if (properties.containsKey(DBCP_REMOVEABANDONED)) {
        basicDataSource.setRemoveAbandoned(Boolean.parseBoolean(properties.getProperty(DBCP_REMOVEABANDONED)));
    }
    if (properties.containsKey(DBCP_REMOVEABANDONEDTIMEOUT)) {
        basicDataSource.setRemoveAbandonedTimeout(
                Integer.parseInt(properties.getProperty(DBCP_REMOVEABANDONEDTIMEOUT)));
    }
    if (properties.containsKey(DBCP_TESTONBORROW)) {
        basicDataSource.setTestOnBorrow(Boolean.parseBoolean(properties.getProperty(DBCP_TESTONBORROW)));
    }
    if (properties.containsKey(DBCP_TESTONCREATE)) {
        basicDataSource.setTestOnReturn(Boolean.parseBoolean(properties.getProperty(DBCP_TESTONCREATE)));
    }
    if (properties.containsKey(DBCP_TESTONRETURN)) {
        basicDataSource.setTestOnReturn(Boolean.parseBoolean(properties.getProperty(DBCP_TESTONRETURN)));
    }
    if (properties.containsKey(DBCP_TESTWHILEIDLE)) {
        basicDataSource.setTestWhileIdle(Boolean.parseBoolean(properties.getProperty(DBCP_TESTWHILEIDLE)));
    }
    if (properties.containsKey(DBCP_TIMEBETWEENEVICTIONRUNSMILLIS)) {
        basicDataSource.setTimeBetweenEvictionRunsMillis(
                Long.parseLong(properties.getProperty(DBCP_TIMEBETWEENEVICTIONRUNSMILLIS)));
    }
    if (properties.containsKey(DBCP_VALIDATIONQUERY)) {
        basicDataSource.setValidationQuery(properties.getProperty(DBCP_VALIDATIONQUERY));
    }
    if (properties.containsKey(DBCP_VALIDATIONQUERYTIMEOUT)) {
        basicDataSource.setValidationQueryTimeout(
                Integer.parseInt(properties.getProperty(DBCP_VALIDATIONQUERYTIMEOUT)));
    }
    return basicDataSource;
}

From source file:mx.com.pixup.portal.dao.CancionParserDaoJdbc.java

public CancionParserDaoJdbc() throws IOException, JDOMException {
    BasicDataSource dataSource = new BasicDataSource();

    //seccion de la DB
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource.setUsername("root");
    dataSource.setPassword("admin");
    //dataSource.setPassword("mysqlroot");
    dataSource.setUrl("jdbc:mysql://localhost:3306/pixup");
    this.dataSource = dataSource;

    //seccion generate XML
    this.xmlParser = new SAXBuilder();
    this.xmlDocumento = this.xmlParser.build(
            "C:\\Users\\JAVA-08\\Documents\\VOSS\\Modulo 5\\pixup-dao-xml\\src\\testXML\\cancion311.xml");
}

From source file:com.miserablemind.butter.bootstrap.RootContext.java

/**
 * Data Source Bean that is configured for JDBC connection using {@link ConfigJDBC}.
 *
 * @return {@link BasicDataSource} bean with a name {@code dataSource}.
 *///  w w w . j  ava 2s  .  co  m
@Bean(name = "dataSource")
public BasicDataSource dataSource() {

    ConfigJDBC configJDBC = this.configSystem.getConfigJDBC();

    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setUsername(configJDBC.getJdbcUsername());
    dataSource.setPassword(configJDBC.getJdbcPassword());
    dataSource.setUrl(configJDBC.getJdbcUrl());
    dataSource.setDriverClassName(configJDBC.getJdbcDriverClassName());

    return dataSource;
}

From source file:eu.peppol.jdbc.OxalisDataSourceFactoryDbcpImplTest.java

@Test
public void testBasicDataSource() throws Exception {

    String jdbcDriverClassPath = globalConfiguration.getJdbcDriverClassPath();
    URLClassLoader urlClassLoader = new URLClassLoader(new URL[] { new URL(jdbcDriverClassPath) },
            Thread.currentThread().getContextClassLoader());

    BasicDataSource basicDataSource = new BasicDataSource();
    basicDataSource.setDriverClassName(globalConfiguration.getJdbcDriverClassName());
    basicDataSource.setUrl(globalConfiguration.getJdbcConnectionURI());
    basicDataSource.setUsername(globalConfiguration.getJdbcUsername());
    basicDataSource.setPassword(globalConfiguration.getJdbcPassword());

    // Does not work in 1.4, fixed in 1.4.1
    basicDataSource.setDriverClassLoader(urlClassLoader);

    try {/*from  w  ww .  j a  va2  s  .c o  m*/
        Connection connection = basicDataSource.getConnection();
        assertNotNull(connection);
        fail("Wuhu! They have finally fixed the bug in DBCP; ignoring the classloader. Consider changing the code!");
    } catch (SQLException e) {
        // As expected when using DBCP 1.4
    }
}

From source file:io.apiman.gateway.engine.impl.DefaultJdbcComponentTest.java

@Test
public void testDataSource() throws Throwable {
    DefaultJdbcComponent component = new DefaultJdbcComponent();

    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName(Driver.class.getName());
    ds.setUsername("sa");
    ds.setPassword("");
    ds.setUrl("jdbc:h2:mem:testDataSource;DB_CLOSE_DELAY=-1");

    try {//w  w  w .ja v a2  s  .co  m
        IJdbcClient client = component.create(ds);
        doAllTests(client);
    } finally {
        try {
            ds.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

From source file:mx.com.pixup.portal.dao.ArtistaGenerateDaoJdbc.java

public ArtistaGenerateDaoJdbc() throws IOException {
    BasicDataSource dataSource = new BasicDataSource();

    //seccion de la DB
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource.setUsername("root");
    dataSource.setPassword("admin");
    //dataSource.setPassword("mysqlroot");
    dataSource.setUrl("jdbc:mysql://localhost:3306/pixup");
    this.dataSource = dataSource;

    //seccion XML
    this.xmlLogico = new Document();
    this.xmlFisico = new XMLOutputter();
    this.archivoFisico = new FileWriter(
            "C:\\Users\\JAVA-08\\Documents\\VOSS\\Modulo 5\\pixup-dao-xml\\src\\testXML\\artistasNuevo.xml");
}

From source file:mx.com.pixup.portal.dao.MunicipioGenerateDaoJdbc.java

public MunicipioGenerateDaoJdbc() throws IOException {
    BasicDataSource dataSource = new BasicDataSource();

    //seccion de la DB
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource.setUsername("root");
    dataSource.setPassword("admin");
    //dataSource.setPassword("mysqlroot");
    dataSource.setUrl("jdbc:mysql://localhost:3306/pixup");
    this.dataSource = dataSource;

    //seccion XML
    this.xmlLogico = new Document();
    this.xmlFisico = new XMLOutputter();
    this.archivoFisico = new FileWriter(
            "C:\\Users\\JAVA-08\\Documents\\VOSS\\Modulo 5\\pixup-dao-xml\\src\\testXML\\municipio1.xml");
}

From source file:mx.com.pixup.portal.dao.ArtistaParserDaoJdbc.java

public ArtistaParserDaoJdbc() throws IOException, JDOMException {
    BasicDataSource dataSource = new BasicDataSource();

    //seccion de la DB
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource.setUsername("root");
    dataSource.setPassword("admin");
    //dataSource.setPassword("mysqlroot");
    dataSource.setUrl("jdbc:mysql://localhost:3306/pixup");
    this.dataSource = dataSource;

    //seccion generate XML
    this.xmlParser = new SAXBuilder();
    this.xmlDocumento = this.xmlParser
            .build("C:\\Users\\JAVA-08\\Documents\\VOSS\\Modulo 5\\pixup-dao-xml\\src\\testXML\\artistas2.xml");
}

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   w  w  w.j a  v a2s .  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:jetsennet.orm.datasource.DbcpDataSourceCreator.java

@Override
public DataSource createDatasource(ConnectionInfo conn, DbPoolInfo props) throws SQLException {
    org.apache.commons.dbcp.BasicDataSource dataSource = new org.apache.commons.dbcp.BasicDataSource();
    dataSource.setDriverClassName(conn.driver);
    dataSource.setUrl(conn.url);
    dataSource.setUsername(conn.user);/*from  w  w  w  .  ja  v  a2 s  .  c  o  m*/
    dataSource.setPassword(conn.pwd);

    String connectionProperties = props.get("connectionProperties");
    if (connectionProperties != null && connectionProperties.trim().length() > 0) {
        dataSource.setConnectionProperties(connectionProperties);
    }
    Boolean defaultAutoCommit = Utils.str2Boolean(props.get("defaultAutoCommit"));
    if (defaultAutoCommit != null) {
        dataSource.setDefaultAutoCommit(defaultAutoCommit);
    }
    Boolean defaultReadOnly = Utils.str2Boolean(props.get("defaultReadOnly"));
    if (defaultReadOnly != null) {
        dataSource.setDefaultReadOnly(defaultReadOnly);
    }
    Integer defaultTransactionIsolation = Utils.strToInteger(props.get("defaultTransactionIsolation"));
    if (defaultTransactionIsolation != null) {
        dataSource.setDefaultTransactionIsolation(defaultTransactionIsolation);
    }
    String defaultCatalog = props.get("defaultCatalog");
    if (defaultCatalog != null && defaultCatalog.trim().length() > 0) {
        dataSource.setDefaultCatalog(defaultCatalog);
    }

    int initialSize = Utils.strToInt(props.get("initialSize"));
    if (initialSize > 0) {
        dataSource.setInitialSize(initialSize);
    }
    int maxActive = Utils.strToInt(props.get("maxActive"));
    if (maxActive > 0) {
        dataSource.setMaxActive(maxActive);
    }
    int maxIdle = Utils.strToInt(props.get("maxIdle"));
    if (maxIdle > 0) {
        dataSource.setMaxIdle(maxIdle);
    }
    int minIdle = Utils.strToInt(props.get("minIdle"));
    if (minIdle > 0) {
        dataSource.setMinIdle(minIdle);
    }
    int maxWait = Utils.strToInt(props.get("maxWait"));
    if (maxWait > 0) {
        dataSource.setMaxWait(maxWait);
    }

    String validationQuery = props.get("validationQuery");
    if (validationQuery != null && validationQuery.trim().length() > 0) {
        dataSource.setValidationQuery(validationQuery);
    }
    Integer validationQueryTimeout = Utils.strToInteger(props.get("validationQueryTimeout"));
    if (validationQueryTimeout != null) {
        dataSource.setValidationQueryTimeout(validationQueryTimeout);
    }
    Boolean testOnBorrow = Utils.str2Boolean(props.get("testOnBorrow"));
    if (testOnBorrow != null) {
        dataSource.setTestOnBorrow(testOnBorrow);
    }
    Boolean testOnReturn = Utils.str2Boolean(props.get("testOnReturn"));
    if (testOnReturn != null) {
        dataSource.setTestOnReturn(testOnReturn);
    }
    Boolean testWhileIdle = Utils.str2Boolean(props.get("testWhileIdle"));
    if (testWhileIdle != null) {
        dataSource.setTestWhileIdle(testWhileIdle);
    }
    Integer timeBetweenEvictionRunsMillis = Utils.strToInteger(props.get("timeBetweenEvictionRunsMillis"));
    if (timeBetweenEvictionRunsMillis != null) {
        dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
    }
    Integer numTestsPerEvictionRun = Utils.strToInteger(props.get("numTestsPerEvictionRun"));
    if (numTestsPerEvictionRun != null) {
        dataSource.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
    }
    int minEvictableIdleTimeMillis = Utils.strToInt(props.get("minEvictableIdleTimeMillis"));
    if (minEvictableIdleTimeMillis > 0) {
        dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
    }

    Boolean removeAbandoned = Utils.str2Boolean(props.get("removeAbandoned"));
    if (removeAbandoned != null) {
        dataSource.setRemoveAbandoned(removeAbandoned);
    }
    int removeAbandonedTimeout = Utils.strToInt(props.get("removeAbandonedTimeout"));
    if (removeAbandonedTimeout > 0) {
        dataSource.setRemoveAbandonedTimeout(removeAbandonedTimeout);
    }
    Boolean logAbandoned = Utils.str2Boolean(props.get("logAbandoned"));
    if (logAbandoned != null) {
        dataSource.setLogAbandoned(logAbandoned);
    }
    return dataSource;
}