本文整理汇总了Java中org.apache.commons.dbcp2.BasicDataSource.setUrl方法的典型用法代码示例。如果您正苦于以下问题:Java BasicDataSource.setUrl方法的具体用法?Java BasicDataSource.setUrl怎么用?Java BasicDataSource.setUrl使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.dbcp2.BasicDataSource
的用法示例。
在下文中一共展示了BasicDataSource.setUrl方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getEntityManagerFactory
import org.apache.commons.dbcp2.BasicDataSource; //导入方法依赖的package包/类
/**
* Returns the singleton EntityManagerFactory instance for accessing the
* default database.
*
* @return the singleton EntityManagerFactory instance
* @throws NamingException
* if a naming exception occurs during initialization
* @throws SQLException
* if a database occurs during initialization
* @throws IOException
*/
public static synchronized EntityManagerFactory getEntityManagerFactory()
throws NamingException, SQLException, IOException {
if (entityManagerFactory == null) {
InitialContext ctx = new InitialContext();
BasicDataSource ds = new BasicDataSource();
JsonNode credentials = readCredentialsFromEnvironment();
ds.setDriverClassName(credentials.get("driver").asText());
ds.setUrl(credentials.get("url").asText());
ds.setUsername(credentials.get("user").asText());
ds.setPassword(credentials.get("password").asText());
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(PersistenceUnitProperties.NON_JTA_DATASOURCE, ds);
entityManagerFactory = Persistence.createEntityManagerFactory(
PERSISTENCE_UNIT_NAME, properties);
}
return entityManagerFactory;
}
示例2: dataSource
import org.apache.commons.dbcp2.BasicDataSource; //导入方法依赖的package包/类
/**
* The following bean configures the database connection. The 'url' property value of "jdbc:derby:directory:jpaserver_derby_files;create=true" indicates that the server should save resources in a
* directory called "jpaserver_derby_files".
*
* A URL to a remote database could also be placed here, along with login credentials and other properties supported by BasicDataSource.
*/
@Bean(destroyMethod = "close")
public DataSource dataSource() {
BasicDataSource retVal = new BasicDataSource();
/*
retVal.setDriver(new org.apache.derby.jdbc.EmbeddedDriver());
retVal.setUrl("jdbc:derby:directory:target/jpaserver_derby_files;create=true");
retVal.setUsername("");
retVal.setPassword("");
* */
try
{
retVal.setDriver(new com.mysql.jdbc.Driver());
}
catch (Exception exc)
{
exc.printStackTrace();
}
retVal.setUrl("jdbc:mysql://localhost:3306/dhis2_fhir");
retVal.setUsername("root");
retVal.setPassword("");
return retVal;
}
示例3: dataSource
import org.apache.commons.dbcp2.BasicDataSource; //导入方法依赖的package包/类
/**
* The following bean configures the database connection. The 'url' property value of "jdbc:derby:directory:jpaserver_derby_files;create=true" indicates that the server should save resources in a
* directory called "jpaserver_derby_files".
*
* A URL to a remote database could also be placed here, along with login credentials and other properties supported by BasicDataSource.
*/
@Bean(destroyMethod = "close")
public DataSource dataSource() {
BasicDataSource retVal = new BasicDataSource();
/*
retVal.setDriver(new org.apache.derby.jdbc.EmbeddedDriver());
retVal.setUrl("jdbc:derby:directory:target/jpaserver_derby_files;create=true");
retVal.setUsername("");
retVal.setPassword("");
*/
try
{
//retVal.setDriver(new com.mysql.jdbc.Driver());
retVal.setDriver(new org.postgresql.Driver());
}
catch (Exception exc)
{
exc.printStackTrace();
}
//retVal.setUrl("jdbc:mysql://localhost:3306/dhis2_fhir");
retVal.setUrl("jdbc:postgresql://localhost:5432/dhis2_fhir");
retVal.setUsername("fhir");
retVal.setPassword("xxxxxxx");
return retVal;
}
示例4: DBManager
import org.apache.commons.dbcp2.BasicDataSource; //导入方法依赖的package包/类
public DBManager() {
ds = new BasicDataSource();
ds.setDriver(new EmbeddedDriver());
ds.setUrl(Constants.JDBC);
flyway = new Flyway();
flyway.setDataSource(ds);
//flyway.clean();
flyway.migrate();
// just to be sure, try to close
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
try {
LOG.info("Closing DB connection...");
ds.close();
LOG.info("DB closed");
} catch (SQLException ex) {
LOG.error("Error closing DB cconnection", ex);
}
}
});
}
示例5: invokeGetDataSource
import org.apache.commons.dbcp2.BasicDataSource; //导入方法依赖的package包/类
public DataSource invokeGetDataSource() {
BasicDataSource bds = new BasicDataSource();
bds.setDriverClassName("com.mysql.jdbc.Driver");
bds.setUrl("jdbc:mysql://127.0.0.1:3306/inst02");
bds.setUsername("root");
bds.setPassword("123456");
bds.setMaxTotal(50);
bds.setInitialSize(20);
bds.setMaxWaitMillis(60000);
bds.setMinIdle(6);
bds.setLogAbandoned(true);
bds.setRemoveAbandonedOnBorrow(true);
bds.setRemoveAbandonedOnMaintenance(true);
bds.setRemoveAbandonedTimeout(1800);
bds.setTestWhileIdle(true);
bds.setTestOnBorrow(false);
bds.setTestOnReturn(false);
bds.setValidationQuery("select 'x' ");
bds.setValidationQueryTimeout(1);
bds.setTimeBetweenEvictionRunsMillis(30000);
bds.setNumTestsPerEvictionRun(20);
return bds;
}
示例6: invokeGetDataSource
import org.apache.commons.dbcp2.BasicDataSource; //导入方法依赖的package包/类
public DataSource invokeGetDataSource() {
BasicDataSource bds = new BasicDataSource();
bds.setDriverClassName("com.mysql.jdbc.Driver");
bds.setUrl("jdbc:mysql://127.0.0.1:3306/inst01");
bds.setUsername("root");
bds.setPassword("123456");
bds.setMaxTotal(50);
bds.setInitialSize(20);
bds.setMaxWaitMillis(60000);
bds.setMinIdle(6);
bds.setLogAbandoned(true);
bds.setRemoveAbandonedOnBorrow(true);
bds.setRemoveAbandonedOnMaintenance(true);
bds.setRemoveAbandonedTimeout(1800);
bds.setTestWhileIdle(true);
bds.setTestOnBorrow(false);
bds.setTestOnReturn(false);
bds.setValidationQuery("select 'x' ");
bds.setValidationQueryTimeout(1);
bds.setTimeBetweenEvictionRunsMillis(30000);
bds.setNumTestsPerEvictionRun(20);
return bds;
}
示例7: dbcp
import org.apache.commons.dbcp2.BasicDataSource; //导入方法依赖的package包/类
@SneakyThrows
private static CloseableDatasource dbcp(Config config) {
int threads = config.getInt("threads");
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(config.getString("driver"));
dataSource.setUrl(config.getString("url"));
dataSource.setUsername(config.getString("user"));
dataSource.setPassword(config.getString("pwd"));
dataSource.setInitialSize(threads);
dataSource.setMinEvictableIdleTimeMillis(120 * 1000);//seconds
DBCPCloseableDataSource ds = new DBCPCloseableDataSource(dataSource);
return ds;
}
示例8: getBasicDataSource
import org.apache.commons.dbcp2.BasicDataSource; //导入方法依赖的package包/类
private static BasicDataSource getBasicDataSource(DatasourceConfiguration configuration) {
BasicDataSource dbcpDataSource = new BasicDataSource();
dbcpDataSource.setDriverClassName(configuration.getDriverClassname());
dbcpDataSource.setUrl(configuration.getUrl());
dbcpDataSource.setUsername(configuration.getUser());
dbcpDataSource.setPassword(configuration.getPassword());
// Enable statement caching (Optional)
dbcpDataSource.setPoolPreparedStatements(true);
dbcpDataSource.setValidationQuery("Select 1 ");
dbcpDataSource.setMaxOpenPreparedStatements(50);
dbcpDataSource.setLifo(true);
dbcpDataSource.setMaxTotal(10);
dbcpDataSource.setInitialSize(2);
return dbcpDataSource;
}
示例9: testOpenConnectionToH2DbHavingAllSupportedPersistenceProperties
import org.apache.commons.dbcp2.BasicDataSource; //导入方法依赖的package包/类
@Test
public void testOpenConnectionToH2DbHavingAllSupportedPersistenceProperties() throws ClassNotFoundException {
// GIVEN
final BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName(H2_DRIVER_CLASS_PROP_VALUE);
ds.setUsername(USERNAME_PROP_VALUE);
ds.setPassword(PASSWORD_PROP_VALUE);
ds.setUrl(H2_CONNECTION_URL_PROP_VALUE);
// WHEN
connection = DatabaseConnectionFactory.openConnection(ds);
// THEN
assertThat(connection, notNullValue());
assertThat(connection, instanceOf(H2Connection.class));
}
示例10: testOpenConnectionToSqliteDbWithoutHavingUsernameAndPasswordProperties
import org.apache.commons.dbcp2.BasicDataSource; //导入方法依赖的package包/类
@Test
public void testOpenConnectionToSqliteDbWithoutHavingUsernameAndPasswordProperties() throws Exception {
// GIVEN
final File dbFile = folder.newFile("test.db");
final BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName(SQLITE_DRIVER_CLASS_PROP_VALUE);
ds.setUrl(SQLITE_CONNECTION_URL_PROP_PREFIX + dbFile.getAbsolutePath());
// WHEN
connection = DatabaseConnectionFactory.openConnection(ds);
// THEN
assertThat(connection, notNullValue());
assertThat(connection.getClass(), equalTo(DatabaseConnection.class));
}
示例11: setupDatabase
import org.apache.commons.dbcp2.BasicDataSource; //导入方法依赖的package包/类
@BeforeClass
public static void setupDatabase() throws Exception {
InputStream inputStream = DeviceDAOImplTest.class.getResourceAsStream("/test.properties");
Properties testProperties = new Properties();
testProperties.load(inputStream);
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);
}
示例12: setupDatabase
import org.apache.commons.dbcp2.BasicDataSource; //导入方法依赖的package包/类
@BeforeClass
public static void setupDatabase() throws Exception {
InputStream inputStream = MMXTopicTagsResourceTest.class.getResourceAsStream("/test.properties");
Properties testProperties = new Properties();
testProperties.load(inputStream);
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);
}
示例13: testExecute
import org.apache.commons.dbcp2.BasicDataSource; //导入方法依赖的package包/类
@Test
public void testExecute() {
final BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("org.h2.Driver");
dataSource.setUrl(
"jdbc:h2:mem:order;" +
"INIT=" +
"RUNSCRIPT FROM 'classpath:database/create.sql'");
DatabaseTester.execute()
.dataSource(dataSource)
.order("INSERT INTO TEST (FIRST_NAME, LAST_NAME) VALUES ('John', 'Doe')")
.order("INSERT INTO TEST (FIRST_NAME, LAST_NAME) VALUES ('Johnathan', 'Smith')")
.build()
.run();
final Collection<Map<String, Object>> result =
new JdbcTemplate(dataSource)
.queryForList("SELECT * FROM TEST");
assertThat(result).hasSize(2);
}
示例14: dataSource
import org.apache.commons.dbcp2.BasicDataSource; //导入方法依赖的package包/类
@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);
basicDataSource.setConnectionProperties("ssl=true;sslfactory=org.postgresql.ssl.NonValidatingFactory;");
return basicDataSource;
}
示例15: getDataSource
import org.apache.commons.dbcp2.BasicDataSource; //导入方法依赖的package包/类
@Bean
public BasicDataSource getDataSource() throws URISyntaxException {
String url = System.getenv("DATABASE_URL");
if (url == null) {
url = "mysql://bugminer:[email protected]:3306/bugminer";
}
URI dbUri = new URI(url);
String username = dbUri.getUserInfo().split(":")[0];
String password = dbUri.getUserInfo().split(":")[1];
String scheme = dbUri.getScheme();
if (scheme.equals("postgres")) {
scheme = "postgresql";
}
String dbUrl = "jdbc:" + scheme + "://" + dbUri.getHost() + ':' + dbUri.getPort() + dbUri.getPath();
BasicDataSource basicDataSource = new BasicDataSource();
basicDataSource.setUrl(dbUrl);
basicDataSource.setUsername(username);
basicDataSource.setPassword(password);
return basicDataSource;
}