List of usage examples for org.apache.commons.dbcp2 BasicDataSource setUsername
public void setUsername(String username)
Sets the #username .
Note: this method currently has no effect once the pool has been initialized.
From source file:dragonrental.backend.DbConfig.java
public DataSource dataSource() { Properties conf = new Properties(); BasicDataSource ds = new BasicDataSource(); try {//from w w w . java 2 s. c o m conf.load(DbConfig.class.getResourceAsStream("/Properties.properties")); } catch (IOException ex) { //log.error("Loading properties has failed!"); } ds.setUrl(conf.getProperty("db.url")); ds.setDriverClassName(conf.getProperty("db.driver")); ds.setUsername(conf.getProperty("db.user")); ds.setPassword(conf.getProperty("db.password")); DatabaseMetaData metaData; ResultSet tables; try (Connection connection = ds.getConnection()) { metaData = connection.getMetaData(); tables = metaData.getTables(null, null, "%", new String[] { "TABLE" }); //checks wheter there is any tables (will not create new tables if it finds ANY table) if (!tables.next()) { new ResourceDatabasePopulator(new ClassPathResource("schema-javadb.sql"), new ClassPathResource("test-data.sql")).execute(ds); } } catch (SQLException ex) { System.out.println("SQL Ex when checking for tables"); System.out.println(ex.getMessage()); } return ds; }
From source file:eu.peppol.persistence.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 ww w . j ava2 s . co m Connection connection = basicDataSource.getConnection(); assertNotNull(connection); } catch (SQLException e) { // As expected when using DBCP 1.4 } }
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 v a2s .com*/ /*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.thoughtworks.go.server.database.H2Database.java
private void configureDataSource(BasicDataSource source, String url) { String databaseUsername = configuration.getUser(); String databasePassword = configuration.getPassword(); LOG.info("[db] Using connection configuration {} [User: {}]", url, databaseUsername); source.setDriverClassName("org.h2.Driver"); source.setUrl(url);//from www. ja v a 2s . com source.setUsername(databaseUsername); source.setPassword(databasePassword); source.setMaxTotal(configuration.getMaxActive()); source.setMaxIdle(configuration.getMaxIdle()); }
From source file:at.rocworks.oa4j.logger.dbs.NoSQLJDBC.java
private void initStorage(BasicDataSource dataSource, int threads) { JDebug.out.info("jdbc init storage..."); dataSource.setDriverClassName(driver); dataSource.setUrl(this.url); dataSource.setMaxTotal(threads);//w w w .j a va2 s . c o m dataSource.setUsername(username); dataSource.setPassword(password); JDebug.out.info("jdbc get connection..."); // open at least one connection to prevent delay on writing/reading Connection conn; try { conn = dataSource.getConnection(); conn.close(); } catch (SQLException ex) { JDebug.StackTrace(Level.SEVERE, ex); } JDebug.out.info("jdbc init storage...done"); }
From source file:com.emc.vipr.sync.filter.TrackingFilter.java
@Override public void parseCustomOptions(CommandLine line) { if (!line.hasOption(DB_URL_OPT)) throw new ConfigurationException("Must provide a database to use the tracking filter"); if (line.hasOption(TABLE_OPT)) tableName = line.getOptionValue(TABLE_OPT); createTable = line.hasOption(CREATE_TABLE_OPT); processAllObjects = line.hasOption(REPROCESS_OPT); if (line.hasOption(META_OPT)) metaTags = Arrays.asList(line.getOptionValue(META_OPT).split(",")); // Initialize a DB connection pool BasicDataSource ds = new BasicDataSource(); ds.setUrl(line.getOptionValue(DB_URL_OPT)); if (line.hasOption(DB_DRIVER_OPT)) ds.setDriverClassName(line.getOptionValue(DB_DRIVER_OPT)); ds.setUsername(line.getOptionValue(DB_USER_OPT)); ds.setPassword(line.getOptionValue(DB_PASSWORD_OPT)); ds.setMaxTotal(200);/*w w w. ja v a 2 s . com*/ ds.setMaxOpenPreparedStatements(180); dataSource = ds; }
From source file:de.micromata.genome.util.runtime.LocalSettingsEnv.java
/** * Parses the ds.//from w w w .jav a 2s.com */ protected void parseDs() { // db.ds.rogerdb.name=RogersOracle // db.ds.rogerdb.drivername=oracle.jdbc.driver.OracleDriver // db.ds.rogerdb.url=jdbc:oracle:thin:@localhost:1521:rogdb // db.ds.rogerdb.username=genome // db.ds.rogerdb.password=genome List<String> dse = localSettings.getKeysPrefixWithInfix("db.ds", "name"); for (String dsn : dse) { String key = dsn + ".name"; String name = localSettings.get(key); if (StringUtils.isBlank(name) == true) { log.error("Name in local-settings is not defined with key: " + key); continue; } key = dsn + ".drivername"; String driverName = localSettings.get(key); if (StringUtils.isBlank(name) == true) { log.error("drivername in local-settings is not defined with key: " + key); continue; } key = dsn + ".url"; String url = localSettings.get(key); if (StringUtils.isBlank(name) == true) { log.error("url in local-settings is not defined with key: " + key); continue; } key = dsn + ".username"; String userName = localSettings.get(key); key = dsn + ".password"; String password = localSettings.get(key); BasicDataSource bd = dataSourceSuplier.get(); bd.setDriverClassName(driverName); bd.setUrl(url); bd.setUsername(userName); bd.setPassword(password); bd.setMaxTotal(localSettings.getIntValue(dsn + ".maxActive", GenericKeyedObjectPoolConfig.DEFAULT_MAX_TOTAL_PER_KEY)); bd.setMaxIdle(localSettings.getIntValue(dsn + ".maxIdle", GenericKeyedObjectPoolConfig.DEFAULT_MAX_IDLE_PER_KEY)); bd.setMinIdle(localSettings.getIntValue(dsn + ".minIdle", GenericKeyedObjectPoolConfig.DEFAULT_MIN_IDLE_PER_KEY)); bd.setMaxWaitMillis(localSettings.getLongValue(dsn + ".maxWait", GenericKeyedObjectPoolConfig.DEFAULT_MAX_WAIT_MILLIS)); bd.setInitialSize(localSettings.getIntValue(dsn + ".intialSize", 0)); bd.setDefaultCatalog(localSettings.get(dsn + ".defaultCatalog", null)); bd.setDefaultAutoCommit(localSettings.getBooleanValue(dsn + ".defaultAutoCommit", true)); bd.setValidationQuery(localSettings.get(dsn + ".validationQuery", null)); bd.setValidationQueryTimeout(localSettings.getIntValue(dsn + ".validationQueryTimeout", -1)); dataSources.put(name, bd); } }
From source file:net.gcolin.simplerepo.search.SearchController.java
public SearchController(ConfigurationManager configManager) throws IOException { this.configManager = configManager; File plugins = new File(configManager.getRoot(), "plugins"); plugins.mkdirs();// www . j a v a 2 s .c o m System.setProperty("derby.system.home", plugins.getAbsolutePath()); BasicDataSource s = new BasicDataSource(); s.setDriverClassName("org.apache.derby.jdbc.EmbeddedDriver"); s.setUrl("jdbc:derby:search" + (new File(plugins, "search").exists() ? "" : ";create=true")); s.setUsername("su"); s.setPassword(""); s.setMaxTotal(10); s.setMinIdle(0); s.setDefaultAutoCommit(true); datasource = s; Set<String> allTables = new HashSet<>(); Connection connection = null; try { try { connection = datasource.getConnection(); connection.setAutoCommit(false); DatabaseMetaData dbmeta = connection.getMetaData(); try (ResultSet rs = dbmeta.getTables(null, null, null, new String[] { "TABLE" })) { while (rs.next()) { allTables.add(rs.getString("TABLE_NAME").toLowerCase()); } } if (!allTables.contains("artifact")) { QueryRunner run = new QueryRunner(); run.update(connection, "CREATE TABLE artifactindex(artifact bigint NOT NULL, version bigint NOT NULL)"); run.update(connection, "INSERT INTO artifactindex (artifact,version) VALUES (?,?)", 1L, 1L); run.update(connection, "CREATE TABLE artifact(id bigint NOT NULL,groupId character varying(120), artifactId character varying(120),CONSTRAINT artifact_pkey PRIMARY KEY (id))"); run.update(connection, "CREATE TABLE artifactversion(artifact_id bigint NOT NULL,id bigint NOT NULL," + "version character varying(100)," + "reponame character varying(30)," + "CONSTRAINT artifactversion_pkey PRIMARY KEY (id)," + "CONSTRAINT fk_artifactversion_artifact_id FOREIGN KEY (artifact_id) REFERENCES artifact (id) )"); run.update(connection, "CREATE TABLE artifacttype(version_id bigint NOT NULL,packaging character varying(20) NOT NULL,classifier character varying(30)," + "CONSTRAINT artifacttype_pkey PRIMARY KEY (version_id,packaging,classifier)," + "CONSTRAINT fk_artifacttype_version FOREIGN KEY (version_id) REFERENCES artifactversion (id))"); run.update(connection, "CREATE INDEX artifactindex ON artifact(groupId,artifactId)"); run.update(connection, "CREATE INDEX artifactgroupindex ON artifact(groupId)"); run.update(connection, "CREATE INDEX artifactversionindex ON artifactversion(version)"); } connection.commit(); } catch (SQLException ex) { connection.rollback(); throw ex; } finally { DbUtils.close(connection); } } catch (SQLException ex) { throw new IOException(ex); } }
From source file:com.dsf.dbxtract.cdc.journal.JournalExecutor.java
/** * @param agentName/* w w w .j a va2s . co m*/ * cdc agent's assigned name * @param zookeeper * connection string to ZooKeeper server * @param handler * {@link JournalHandler} * @param source * {@link Source} */ public JournalExecutor(String agentName, String zookeeper, JournalHandler handler, Source source) { logPrefix = agentName + " :: "; if (logger.isDebugEnabled()) logger.debug(logPrefix + "Creating executor for " + handler + " and " + source); this.agentName = agentName; this.zookeeper = zookeeper; this.handler = handler; this.source = source; BasicDataSource ds = dataSources.get(source); if (ds == null) { if (logger.isDebugEnabled()) logger.debug(agentName + " :: setting up a connection pool for " + source.toString()); ds = new BasicDataSource(); ds.setDriverClassName(source.getDriver()); ds.setUsername(source.getUser()); ds.setPassword(source.getPassword()); ds.setUrl(source.getConnection()); dataSources.put(source, ds); } if (statistics == null) statistics = new Statistics(); }
From source file:com.emc.vipr.sync.source.AtmosSource.java
@Override public void parseCustomOptions(CommandLine line) { AtmosUtil.AtmosUri atmosUri = AtmosUtil.parseUri(sourceUri); endpoints = atmosUri.endpoints;//ww w . ja va2 s. c om uid = atmosUri.uid; secret = atmosUri.secret; namespaceRoot = atmosUri.rootPath; if (line.hasOption(SOURCE_OIDLIST_OPTION)) oidFile = line.getOptionValue(SOURCE_OIDLIST_OPTION); if (line.hasOption(SOURCE_NAMELIST_OPTION)) nameFile = line.getOptionValue(SOURCE_NAMELIST_OPTION); if (line.hasOption(SOURCE_SQLQUERY_OPTION)) { query = line.getOptionValue(SOURCE_SQLQUERY_OPTION); // Initialize a connection pool BasicDataSource ds = new BasicDataSource(); ds.setUrl(line.getOptionValue(JDBC_URL_OPT)); if (line.hasOption(JDBC_DRIVER_OPT)) ds.setDriverClassName(line.getOptionValue(JDBC_DRIVER_OPT)); ds.setUsername(line.getOptionValue(JDBC_USER_OPT)); ds.setPassword(line.getOptionValue(JDBC_PASSWORD_OPT)); ds.setMaxTotal(200); ds.setMaxOpenPreparedStatements(180); setDataSource(ds); } deleteTags = line.hasOption(DELETE_TAGS_OPT); }