/** * Create a new DriverManagerDataSource with the given JDBC URL, * not specifying a username or password for JDBC access. * @param url the JDBC URL to use for accessing the DriverManager * @see java.sql.DriverManager#getConnection(String) */ public DriverManagerDataSource(String url) { setUrl(url); }
/** * Create a new DriverManagerDataSource with the given JDBC URL, * not specifying a username or password for JDBC access. * @param url the JDBC URL to use for accessing the DriverManager * @param conProps the JDBC connection properties * @see java.sql.DriverManager#getConnection(String) */ public DriverManagerDataSource(String url, Properties conProps) { setUrl(url); setConnectionProperties(conProps); }
/** * Create a new DriverManagerDataSource with the given standard * DriverManager parameters. * @param url the JDBC URL to use for accessing the DriverManager * @param username the JDBC username to use for accessing the DriverManager * @param password the JDBC password to use for accessing the DriverManager * @see java.sql.DriverManager#getConnection(String, String, String) */ public DriverManagerDataSource(String url, String username, String password) { setUrl(url); setUsername(username); setPassword(password); }
@Bean @Autowired public DataSource dataSource(DatabasePopulator populator) { final DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName")); dataSource.setUrl(env.getProperty("jdbc.url")); dataSource.setUsername(env.getProperty("jdbc.username")); dataSource.setPassword(env.getProperty("jdbc.password")); DatabasePopulatorUtils.execute(populator, dataSource); return dataSource; }
/** * DataSource definition for database connection. Settings are read from * the application.properties file (using the env object). */ @Bean public DataSource dataSource() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName(env.getProperty("db.driver")); dataSource.setUrl(env.getProperty("db.url")); dataSource.setUsername(env.getProperty("db.username")); dataSource.setPassword(env.getProperty("db.password")); return dataSource; }
@Bean public DataSource dataSource() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setUrl(jdbcUrl); dataSource.setUsername(jdbcUsername); dataSource.setPassword(jdbcPassword); return dataSource; }
/** * 获取新的数据源 * * @return */ public static DataSource getNewDataSource() { if (newDataSource == null) synchronized (TaleUtils.class) { if (newDataSource == null) { Properties properties = TaleUtils.getPropFromFile("application-default.properties"); if (properties.size() == 0) { return newDataSource; } DriverManagerDataSource managerDataSource = new DriverManagerDataSource(); managerDataSource.setDriverClassName("com.mysql.jdbc.Driver"); managerDataSource.setPassword(properties.getProperty("spring.datasource.password")); String str = "jdbc:mysql://" + properties.getProperty("spring.datasource.url") + "/" + properties.getProperty("spring.datasource.dbname") + "?useUnicode=true&characterEncoding=utf-8&useSSL=false"; managerDataSource.setUrl(str); managerDataSource.setUsername(properties.getProperty("spring.datasource.username")); newDataSource = managerDataSource; } } return newDataSource; }
@Bean public DataSource dataSource() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName(DB_DRIVER); dataSource.setUrl(DB_URL); dataSource.setUsername(DB_USERNAME); dataSource.setPassword(DB_PASSWORD); return dataSource; }
@Test public void testUsageWithConnectionProperties() throws Exception { final String jdbcUrl = "url"; final Properties connProps = new Properties(); connProps.setProperty("myProp", "myValue"); connProps.setProperty("yourProp", "yourValue"); connProps.setProperty("user", "uname"); connProps.setProperty("password", "pwd"); class TestDriverManagerDataSource extends DriverManagerDataSource { @Override protected Connection getConnectionFromDriverManager(String url, Properties props) { assertEquals(jdbcUrl, url); assertEquals("uname", props.getProperty("user")); assertEquals("pwd", props.getProperty("password")); assertEquals("myValue", props.getProperty("myProp")); assertEquals("yourValue", props.getProperty("yourProp")); return connection; } } DriverManagerDataSource ds = new TestDriverManagerDataSource(); //ds.setDriverClassName("foobar"); ds.setUrl(jdbcUrl); ds.setConnectionProperties(connProps); Connection actualCon = ds.getConnection(); assertTrue(actualCon == connection); assertTrue(ds.getUrl().equals(jdbcUrl)); }
ds.setUrl(jdbcUrl); ds.setUsername(uname); ds.setPassword(pwd);
@Test public void testStandardUsage() throws Exception { final String jdbcUrl = "url"; final String uname = "uname"; final String pwd = "pwd"; class TestDriverManagerDataSource extends DriverManagerDataSource { @Override protected Connection getConnectionFromDriverManager(String url, Properties props) { assertEquals(jdbcUrl, url); assertEquals(uname, props.getProperty("user")); assertEquals(pwd, props.getProperty("password")); return connection; } } DriverManagerDataSource ds = new TestDriverManagerDataSource(); //ds.setDriverClassName("foobar"); ds.setUrl(jdbcUrl); ds.setUsername(uname); ds.setPassword(pwd); Connection actualCon = ds.getConnection(); assertTrue(actualCon == connection); assertTrue(ds.getUrl().equals(jdbcUrl)); assertTrue(ds.getPassword().equals(pwd)); assertTrue(ds.getUsername().equals(uname)); }
/** * 获取新的数据源 * * @return */ public static DataSource getNewDataSource() { if (newDataSource == null) synchronized (TaleUtils.class) { if (newDataSource == null) { Properties properties = TaleUtils.getPropFromFile("application-default.properties"); if (properties.size() == 0) { return newDataSource; } DriverManagerDataSource managerDataSource = new DriverManagerDataSource(); managerDataSource.setDriverClassName("com.mysql.jdbc.Driver"); managerDataSource.setPassword(properties.getProperty("spring.datasource.password")); String str = "jdbc:mysql://" + properties.getProperty("spring.datasource.url") + "/" + properties.getProperty("spring.datasource.dbname") + "?useUnicode=true&characterEncoding=utf-8&useSSL=false"; managerDataSource.setUrl(str); managerDataSource.setUsername(properties.getProperty("spring.datasource.username")); newDataSource = managerDataSource; } } return newDataSource; }
/** * Create a new DriverManagerDataSource with the given JDBC URL, * not specifying a username or password for JDBC access. * @param url the JDBC URL to use for accessing the DriverManager * @param conProps the JDBC connection properties * @see java.sql.DriverManager#getConnection(String) */ public DriverManagerDataSource(String url, Properties conProps) { setUrl(url); setConnectionProperties(conProps); }
@Bean public DataSource dataSource() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("org.h2.Driver"); dataSource.setUrl("jdbc:h2:tcp://localhost/~/test"); dataSource.setUsername("sa"); dataSource.setPassword(""); return dataSource; }
@Bean public DriverManagerDataSource dataSource(){ DriverManagerDataSource ret = new DriverManagerDataSource(); ret.setDriverClassName(environment.getRequiredProperty("driverClassName")); ret.setUsername(environment.getRequiredProperty("username")); ret.setPassword(environment.getRequiredProperty("password")); ret.setUrl(environment.getRequiredProperty("url")); return ret; }
@Bean public DataSource dataSource(){ DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://localhost:3306/db"); dataSource.setUsername("user"); dataSource.setPassword("password"); return null; }
@Bean public DataSource dataSource() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("org.postgresql.Driver"); dataSource.setUrl("jdbc:postgresql://localhost:port/DB_NAME"); dataSource.setUsername("root"); dataSource.setPassword(""); return dataSource; }
@Bean public DataSource dataSource() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("org.h2.Driver"); dataSource.setUrl(H2_URL + ";DB_CLOSE_DELAY=-1"); return dataSource; }
@Bean public DataSource dataSource(){ DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("org.h2.Driver"); dataSource.setUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1"); return dataSource; }
private DataSource getDataSource() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("org.hsqldb.jdbcDriver"); dataSource.setUrl("jdbc:hsqldb:mem:hspcl"); dataSource.setUsername("sa"); dataSource.setPassword(""); return dataSource; }