Java Code Examples for org.springframework.jdbc.datasource.SimpleDriverDataSource

The following examples show how to use org.springframework.jdbc.datasource.SimpleDriverDataSource. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: spring-analysis-note   Source File: EmbeddedDatabaseFactory.java    License: MIT License 6 votes vote down vote up
/**
 * Hook to shutdown the embedded database. Subclasses may call this method
 * to force shutdown.
 * <p>After calling, {@link #getDataSource()} returns {@code null}.
 * <p>Does nothing if no embedded database has been initialized.
 */
protected void shutdownDatabase() {
	if (this.dataSource != null) {
		if (logger.isInfoEnabled()) {
			if (this.dataSource instanceof SimpleDriverDataSource) {
				logger.info(String.format("Shutting down embedded database: url='%s'",
					((SimpleDriverDataSource) this.dataSource).getUrl()));
			}
			else {
				logger.info(String.format("Shutting down embedded database '%s'", this.databaseName));
			}
		}
		if (this.databaseConfigurer != null) {
			this.databaseConfigurer.shutdown(this.dataSource, this.databaseName);
		}
		this.dataSource = null;
	}
}
 
Example 2
Source Project: java-technology-stack   Source File: EmbeddedDatabaseFactory.java    License: MIT License 6 votes vote down vote up
/**
 * Hook to shutdown the embedded database. Subclasses may call this method
 * to force shutdown.
 * <p>After calling, {@link #getDataSource()} returns {@code null}.
 * <p>Does nothing if no embedded database has been initialized.
 */
protected void shutdownDatabase() {
	if (this.dataSource != null) {
		if (logger.isInfoEnabled()) {
			if (this.dataSource instanceof SimpleDriverDataSource) {
				logger.info(String.format("Shutting down embedded database: url='%s'",
					((SimpleDriverDataSource) this.dataSource).getUrl()));
			}
			else {
				logger.info(String.format("Shutting down embedded database '%s'", this.databaseName));
			}
		}
		if (this.databaseConfigurer != null) {
			this.databaseConfigurer.shutdown(this.dataSource, this.databaseName);
		}
		this.dataSource = null;
	}
}
 
Example 3
Source Project: FastSQL   Source File: SQLFactory.java    License: Apache License 2.0 6 votes vote down vote up
public static SQLFactory createUseSimpleDateSource(Driver driver, String url, String username, String password) {
    SQLFactory sqlFactory = new SQLFactory();
    if (url.contains("jdbc:mysql:")) {
        sqlFactory.setDataSourceType(DataSourceType.MY_SQL);
    }
    if (url.contains("jdbc:oracle:")) {
        sqlFactory.setDataSourceType(DataSourceType.ORACLE);
    }

    if (url.contains("jdbc:postgresql:")) {
        sqlFactory.setDataSourceType(DataSourceType.POSTGRESQL);
    }

    SimpleDriverDataSource dataSource = new SimpleDriverDataSource(driver, url, username, password);
    sqlFactory.setDataSource(dataSource);
    return sqlFactory;
}
 
Example 4
DatabaseCreator(String url, String username, String password) {
    String databaseUrlPattern = getDatabaseUrlPattern(url);
    Pattern jdbcBaseUrlWithDbNamePattern = Pattern.compile(databaseUrlPattern);
    Matcher matcher = jdbcBaseUrlWithDbNamePattern.matcher(url);

    if (!matcher.matches()) {
        throw new IllegalArgumentException(url + " does not match " + databaseUrlPattern);
    }

    String jdbcBaseUrl = matcher.group("jdbcBaseUrl");
    String databaseName = matcher.group("databaseName");
    databaseExistsQuery = String.format("SELECT count(*) FROM sys.databases WHERE name='%s'", databaseName);
    createDatabaseQuery = String.format("CREATE DATABASE %s", databaseName);
    this.template = new JdbcTemplate(
            new SimpleDriverDataSource(getDriver(jdbcBaseUrl), jdbcBaseUrl, username, password));
}
 
Example 5
Source Project: lams   Source File: EmbeddedDatabaseFactory.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Hook to shutdown the embedded database. Subclasses may call this method
 * to force shutdown.
 * <p>After calling, {@link #getDataSource()} returns {@code null}.
 * <p>Does nothing if no embedded database has been initialized.
 */
protected void shutdownDatabase() {
	if (this.dataSource != null) {

		if (logger.isInfoEnabled()) {
			if (this.dataSource instanceof SimpleDriverDataSource) {
				logger.info(String.format("Shutting down embedded database: url='%s'",
					((SimpleDriverDataSource) this.dataSource).getUrl()));
			}
			else {
				logger.info(String.format("Shutting down embedded database '%s'", this.databaseName));
			}
		}

		this.databaseConfigurer.shutdown(this.dataSource, this.databaseName);
		this.dataSource = null;
	}
}
 
Example 6
/**
 * Hook to shutdown the embedded database. Subclasses may call this method
 * to force shutdown.
 * <p>After calling, {@link #getDataSource()} returns {@code null}.
 * <p>Does nothing if no embedded database has been initialized.
 */
protected void shutdownDatabase() {
	if (this.dataSource != null) {

		if (logger.isInfoEnabled()) {
			if (this.dataSource instanceof SimpleDriverDataSource) {
				logger.info(String.format("Shutting down embedded database: url='%s'",
					((SimpleDriverDataSource) this.dataSource).getUrl()));
			}
			else {
				logger.info(String.format("Shutting down embedded database '%s'", this.databaseName));
			}
		}

		this.databaseConfigurer.shutdown(this.dataSource, this.databaseName);
		this.dataSource = null;
	}
}
 
Example 7
public static void main(String[] args) {
    ExperimentDAO source = null;
    ExperimentDAO target = null;
    try {
        source = new ExperimentDAOImpl(
                new SimpleDriverDataSource(new org.hsqldb.jdbc.JDBCDriver(), "jdbc:hsqldb:file:" + SOURCE_DB_PATH));
        source.initialize();
        target = new ExperimentDAOImpl(
                new SimpleDriverDataSource(new org.hsqldb.jdbc.JDBCDriver(), "jdbc:hsqldb:file:" + TARGET_DB_PATH));
        target.initialize();
        performMigration(source, target);
    } finally {
        IOUtils.closeQuietly(source);
        IOUtils.closeQuietly(target);
    }
}
 
Example 8
Source Project: spring-analysis-note   Source File: EmbeddedDatabaseFactory.java    License: MIT License 5 votes vote down vote up
/**
 * Hook to initialize the embedded database.
 * <p>If the {@code generateUniqueDatabaseName} flag has been set to {@code true},
 * the current value of the {@linkplain #setDatabaseName database name} will
 * be overridden with an auto-generated name.
 * <p>Subclasses may call this method to force initialization; however,
 * this method should only be invoked once.
 * <p>After calling this method, {@link #getDataSource()} returns the
 * {@link DataSource} providing connectivity to the database.
 */
protected void initDatabase() {
	if (this.generateUniqueDatabaseName) {
		setDatabaseName(UUID.randomUUID().toString());
	}

	// Create the embedded database first
	if (this.databaseConfigurer == null) {
		this.databaseConfigurer = EmbeddedDatabaseConfigurerFactory.getConfigurer(EmbeddedDatabaseType.HSQL);
	}
	this.databaseConfigurer.configureConnectionProperties(
			this.dataSourceFactory.getConnectionProperties(), this.databaseName);
	this.dataSource = this.dataSourceFactory.getDataSource();

	if (logger.isInfoEnabled()) {
		if (this.dataSource instanceof SimpleDriverDataSource) {
			SimpleDriverDataSource simpleDriverDataSource = (SimpleDriverDataSource) this.dataSource;
			logger.info(String.format("Starting embedded database: url='%s', username='%s'",
					simpleDriverDataSource.getUrl(), simpleDriverDataSource.getUsername()));
		}
		else {
			logger.info(String.format("Starting embedded database '%s'", this.databaseName));
		}
	}

	// Now populate the database
	if (this.databasePopulator != null) {
		try {
			DatabasePopulatorUtils.execute(this.databasePopulator, this.dataSource);
		}
		catch (RuntimeException ex) {
			// failed to populate, so leave it as not initialized
			shutdownDatabase();
			throw ex;
		}
	}
}
 
Example 9
Source Project: java-technology-stack   Source File: EmbeddedDatabaseFactory.java    License: MIT License 5 votes vote down vote up
/**
 * Hook to initialize the embedded database.
 * <p>If the {@code generateUniqueDatabaseName} flag has been set to {@code true},
 * the current value of the {@linkplain #setDatabaseName database name} will
 * be overridden with an auto-generated name.
 * <p>Subclasses may call this method to force initialization; however,
 * this method should only be invoked once.
 * <p>After calling this method, {@link #getDataSource()} returns the
 * {@link DataSource} providing connectivity to the database.
 */
protected void initDatabase() {
	if (this.generateUniqueDatabaseName) {
		setDatabaseName(UUID.randomUUID().toString());
	}

	// Create the embedded database first
	if (this.databaseConfigurer == null) {
		this.databaseConfigurer = EmbeddedDatabaseConfigurerFactory.getConfigurer(EmbeddedDatabaseType.HSQL);
	}
	this.databaseConfigurer.configureConnectionProperties(
			this.dataSourceFactory.getConnectionProperties(), this.databaseName);
	this.dataSource = this.dataSourceFactory.getDataSource();

	if (logger.isInfoEnabled()) {
		if (this.dataSource instanceof SimpleDriverDataSource) {
			SimpleDriverDataSource simpleDriverDataSource = (SimpleDriverDataSource) this.dataSource;
			logger.info(String.format("Starting embedded database: url='%s', username='%s'",
					simpleDriverDataSource.getUrl(), simpleDriverDataSource.getUsername()));
		}
		else {
			logger.info(String.format("Starting embedded database '%s'", this.databaseName));
		}
	}

	// Now populate the database
	if (this.databasePopulator != null) {
		try {
			DatabasePopulatorUtils.execute(this.databasePopulator, this.dataSource);
		}
		catch (RuntimeException ex) {
			// failed to populate, so leave it as not initialized
			shutdownDatabase();
			throw ex;
		}
	}
}
 
Example 10
Source Project: herd-mdl   Source File: HerdMetastoreConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean (name = "hiveJdbcTemplate")
public JdbcTemplate hiveJdbcTemplate() {
    SimpleDriverDataSource dataSource = new SimpleDriverDataSource(
        new HiveDriver()
        , HIVE_URL
        , HIVE_USER
        , HIVE_PASSWORD
    );
    return new JdbcTemplate( dataSource );
}
 
Example 11
Source Project: football-events   Source File: FootballEcosystem.java    License: MIT License 5 votes vote down vote up
private void connectorCreated() {
    // create database and table
    postgres = new JdbcTemplate(new SimpleDriverDataSource(new Driver(),
        "jdbc:postgresql://postgres:5432/postgres", "postgres", "postgres"));
    createPlayersTable();
    createConnector("http://connect:8083/connectors/", "football-connector.json");
    sleep(2000); // some time to init the connector
}
 
Example 12
Source Project: activiti6-boot2   Source File: DatabaseConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public DataSource dataSource() {
  SimpleDriverDataSource ds = new SimpleDriverDataSource();
  ds.setDriverClass(org.h2.Driver.class);

  // Connection settings
  ds.setUrl("jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000");
  ds.setUsername("sa");

  return ds;
}
 
Example 13
@Bean
public DataSource dataSource() {
  SimpleDriverDataSource ds = new SimpleDriverDataSource();
  ds.setDriverClass(org.h2.Driver.class);

  // Connection settings
  ds.setUrl("jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000");
  ds.setUsername("sa");

  return ds;
}
 
Example 14
Source Project: FastSQL   Source File: SQLFactoryTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void crateFactory() {
    DataSource dataSource = new SimpleDriverDataSource();

    SQLFactory sqlFactory = new SQLFactory();
    sqlFactory.setDataSource(dataSource);
}
 
Example 15
Source Project: FastSQL   Source File: SQLFactoryTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
    public void crateSQL() {
        DataSource dataSource = new SimpleDriverDataSource();

        SQLFactory sqlFactory = new SQLFactory();
        sqlFactory.setDataSource(dataSource);

        SQL sql = sqlFactory.createSQL();
//        Student student = sql.SELECT("*").FROM("student").WHERE("id=101").queryOne(Student.class);
    }
 
Example 16
@Override
protected void setUp() throws Exception {
    this.dataSource =
            new SimpleDriverDataSource(
                    new org.hsqldb.jdbcDriver(), "jdbc:hsqldb:mem:CasTest", "sa", "");

    this.jdbcTemplate = new JdbcTemplate(this.dataSource);
    this.jdbcTemplate.execute(
            "CREATE TABLE UP_PERSON_DIR (USER_NAME VARCHAR(1000), ENCRPTD_PSWD VARCHAR(1000))");

    this.userPasswordDao = new PortalPersonDirUserPasswordDao();
    this.userPasswordDao.setDataSource(this.dataSource);
}
 
Example 17
Source Project: lams   Source File: EmbeddedDatabaseFactory.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Hook to initialize the embedded database.
 * <p>If the {@code generateUniqueDatabaseName} flag has been set to {@code true},
 * the current value of the {@linkplain #setDatabaseName database name} will
 * be overridden with an auto-generated name.
 * <p>Subclasses may call this method to force initialization; however,
 * this method should only be invoked once.
 * <p>After calling this method, {@link #getDataSource()} returns the
 * {@link DataSource} providing connectivity to the database.
 */
protected void initDatabase() {
	if (this.generateUniqueDatabaseName) {
		setDatabaseName(UUID.randomUUID().toString());
	}

	// Create the embedded database first
	if (this.databaseConfigurer == null) {
		this.databaseConfigurer = EmbeddedDatabaseConfigurerFactory.getConfigurer(EmbeddedDatabaseType.HSQL);
	}
	this.databaseConfigurer.configureConnectionProperties(
			this.dataSourceFactory.getConnectionProperties(), this.databaseName);
	this.dataSource = this.dataSourceFactory.getDataSource();

	if (logger.isInfoEnabled()) {
		if (this.dataSource instanceof SimpleDriverDataSource) {
			SimpleDriverDataSource simpleDriverDataSource = (SimpleDriverDataSource) this.dataSource;
			logger.info(String.format("Starting embedded database: url='%s', username='%s'",
				simpleDriverDataSource.getUrl(), simpleDriverDataSource.getUsername()));
		}
		else {
			logger.info(String.format("Starting embedded database '%s'", this.databaseName));
		}
	}

	// Now populate the database
	if (this.databasePopulator != null) {
		try {
			DatabasePopulatorUtils.execute(this.databasePopulator, this.dataSource);
		}
		catch (RuntimeException ex) {
			// failed to populate, so leave it as not initialized
			shutdownDatabase();
			throw ex;
		}
	}
}
 
Example 18
Source Project: jwala   Source File: TestJpaConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public DataSource getDataSource() {
    return new SimpleDriverDataSource(new Driver(),
            "jdbc:h2:mem:test-services;DB_CLOSE_DELAY=-1;LOCK_MODE=0",
            "sa",
            "");
}
 
Example 19
@Bean
@ConfigurationProperties("spring.datasource")
@LiquibaseDataSource
public DataSource dataSource(DataSourceProperties properties) {
	return new SimpleDriverDataSource(new org.postgresql.Driver(), properties.getUrl(),
			properties.getDataUsername(), properties.getDataPassword());
}
 
Example 20
@Bean
@ConfigurationProperties("spring.datasource")
@LiquibaseDataSource
public DataSource dataSource(DataSourceProperties properties) {
	return new SimpleDriverDataSource(new org.postgresql.Driver(), properties.getUrl(),
			properties.getDataUsername(), properties.getDataPassword());
}
 
Example 21
/**
 * Hook to initialize the embedded database.
 * <p>If the {@code generateUniqueDatabaseName} flag has been set to {@code true},
 * the current value of the {@linkplain #setDatabaseName database name} will
 * be overridden with an auto-generated name.
 * <p>Subclasses may call this method to force initialization; however,
 * this method should only be invoked once.
 * <p>After calling this method, {@link #getDataSource()} returns the
 * {@link DataSource} providing connectivity to the database.
 */
protected void initDatabase() {
	if (this.generateUniqueDatabaseName) {
		setDatabaseName(UUID.randomUUID().toString());
	}

	// Create the embedded database first
	if (this.databaseConfigurer == null) {
		this.databaseConfigurer = EmbeddedDatabaseConfigurerFactory.getConfigurer(EmbeddedDatabaseType.HSQL);
	}
	this.databaseConfigurer.configureConnectionProperties(
			this.dataSourceFactory.getConnectionProperties(), this.databaseName);
	this.dataSource = this.dataSourceFactory.getDataSource();

	if (logger.isInfoEnabled()) {
		if (this.dataSource instanceof SimpleDriverDataSource) {
			SimpleDriverDataSource simpleDriverDataSource = (SimpleDriverDataSource) this.dataSource;
			logger.info(String.format("Starting embedded database: url='%s', username='%s'",
				simpleDriverDataSource.getUrl(), simpleDriverDataSource.getUsername()));
		}
		else {
			logger.info(String.format("Starting embedded database '%s'", this.databaseName));
		}
	}

	// Now populate the database
	if (this.databasePopulator != null) {
		try {
			DatabasePopulatorUtils.execute(this.databasePopulator, this.dataSource);
		}
		catch (RuntimeException ex) {
			// failed to populate, so leave it as not initialized
			shutdownDatabase();
			throw ex;
		}
	}
}
 
Example 22
Source Project: flowable-engine   Source File: EngineConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public DataSource dataSource() {
    SimpleDriverDataSource ds = new SimpleDriverDataSource();
    ds.setDriverClass(jdbcDriver);

    // Connection settings
    ds.setUrl("jdbc:h2:mem:flowable;DB_CLOSE_DELAY=1000");
    ds.setUsername(jdbcUsername);
    ds.setPassword(jdbcPassword);

    return ds;
}
 
Example 23
Source Project: flowable-engine   Source File: EngineConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public DataSource dataSource() {
    SimpleDriverDataSource ds = new SimpleDriverDataSource();
    ds.setDriverClass(jdbcDriver);

    // Connection settings
    ds.setUrl("jdbc:h2:mem:flowable;DB_CLOSE_DELAY=1000");
    ds.setUsername(jdbcUsername);
    ds.setPassword(jdbcPassword);

    return ds;
}
 
Example 24
Source Project: flowable-engine   Source File: DatabaseConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public DataSource dataSource() {
    SimpleDriverDataSource ds = new SimpleDriverDataSource();
    ds.setDriverClass(org.h2.Driver.class);

    // Connection settings
    ds.setUrl("jdbc:h2:mem:flowable;DB_CLOSE_DELAY=1000");
    ds.setUsername("sa");

    return ds;
}
 
Example 25
Source Project: flowable-engine   Source File: EngineConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public DataSource dataSource() {
    SimpleDriverDataSource ds = new SimpleDriverDataSource();
    ds.setDriverClass(jdbcDriver);

    // Connection settings
    ds.setUrl("jdbc:h2:mem:flowable;DB_CLOSE_DELAY=1000");
    ds.setUsername(jdbcUsername);
    ds.setPassword(jdbcPassword);

    return ds;
}
 
Example 26
Source Project: flowable-engine   Source File: EngineConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public DataSource dataSource() {
    SimpleDriverDataSource ds = new SimpleDriverDataSource();
    ds.setDriverClass(jdbcDriver);

    // Connection settings
    ds.setUrl("jdbc:h2:mem:flowable;DB_CLOSE_DELAY=1000");
    ds.setUsername(jdbcUsername);
    ds.setPassword(jdbcPassword);

    return ds;
}
 
Example 27
/**
 * 定义数据源
 * @return
 * @throws ClassNotFoundException
 */
@Bean
public DataSource dataSource() throws ClassNotFoundException {
    SimpleDriverDataSource simpleDriverDataSource = new SimpleDriverDataSource();
    simpleDriverDataSource.setDriverClass((Class<? extends Driver>) Class.forName("org.h2.Driver"));
    simpleDriverDataSource.setUrl("jdbc:h2:mem:aia-chapter7;DB_CLOSE_DELAY=1000");
    simpleDriverDataSource.setUsername("sa");
    simpleDriverDataSource.setPassword("");
    return simpleDriverDataSource;
}
 
Example 28
Source Project: cloudbreak   Source File: DatabaseConfig.java    License: Apache License 2.0 5 votes vote down vote up
private void createSchemaIfNeeded(String dbType, String dbAddress, String dbName, String dbUser, String dbPassword, String dbSchema)
        throws SQLException {
    if (!DEFAULT_SCHEMA_NAME.equals(dbSchema)) {
        SimpleDriverDataSource ds = new SimpleDriverDataSource();
        ds.setDriverClass(Driver.class);
        ds.setUrl(String.format("jdbc:%s://%s/%s", dbType, dbAddress, dbName));
        try (Connection conn = ds.getConnection(dbUser, dbPassword); Statement statement = conn.createStatement()) {
            statement.execute("CREATE SCHEMA IF NOT EXISTS " + dbSchema);
        }
    }
}
 
Example 29
Source Project: cloudbreak   Source File: DatabaseUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static void createSchemaIfNeeded(String dbType, String dbAddress, String dbName, String dbUser, String dbPassword, String dbSchema)
        throws SQLException {
    if (!DEFAULT_SCHEMA_NAME.equals(dbSchema)) {
        SimpleDriverDataSource ds = new SimpleDriverDataSource();
        ds.setDriverClass(Driver.class);
        ds.setUrl(String.format("jdbc:%s://%s/%s", dbType, dbAddress, dbName));
        try (Connection conn = ds.getConnection(dbUser, dbPassword); Statement statement = conn.createStatement()) {
            statement.execute("CREATE SCHEMA IF NOT EXISTS " + dbSchema);
        }
    }
}
 
Example 30
private static DataSource newSimpleDriverDataSource(String jdbcUrl) {
	try {
		return new SimpleDriverDataSource(DriverManager.getDriver(jdbcUrl), jdbcUrl);
	} catch (SQLException e) {
		throw new RuntimeException("Unable to construct DataSource", e);
	}
}