Example usage for org.springframework.jdbc.datasource.init ResourceDatabasePopulator ResourceDatabasePopulator

List of usage examples for org.springframework.jdbc.datasource.init ResourceDatabasePopulator ResourceDatabasePopulator

Introduction

In this page you can find the example usage for org.springframework.jdbc.datasource.init ResourceDatabasePopulator ResourceDatabasePopulator.

Prototype

public ResourceDatabasePopulator(boolean continueOnError, boolean ignoreFailedDrops,
        @Nullable String sqlScriptEncoding, Resource... scripts) 

Source Link

Document

Construct a new ResourceDatabasePopulator with the supplied values.

Usage

From source file:ctv.stageissue.Application.java

@Bean
public DataSource dataSource() {
    HikariConfig config = new HikariConfig();
    config.setDataSourceClassName("org.h2.jdbcx.JdbcDataSource");
    config.setConnectionTestQuery("VALUES 1");
    config.addDataSourceProperty("url", "jdbc:h2:~/test");
    config.addDataSourceProperty("user", "sa");
    config.addDataSourceProperty("password", "");
    HikariDataSource dataSource = new HikariDataSource(config);

    DataSourceInitializer initializer = new DataSourceInitializer();
    initializer.setDataSource(dataSource);
    initializer.setDatabasePopulator(/*from  w  w  w. j  a v a 2 s  . com*/
            new ResourceDatabasePopulator(false, false, null, new ClassPathResource("model.sql")));
    initializer.setEnabled(true);
    initializer.afterPropertiesSet();

    return dataSource;
}

From source file:org.tightblog.ui.restapi.InstallerController.java

@RequestMapping(value = "/create")
public ModelAndView createDatabaseTables(HttpServletRequest request, HttpServletResponse response)
        throws IOException {
    if (dynamicProperties.isDatabaseReady()) {
        response.sendRedirect(request.getContextPath() + "/");
        return null;
    }//from w  ww.  j  ava2  s. c o  m
    Map<String, Object> map = initializeMap();
    List<String> messageList = new ArrayList<>(100);
    map.put("messages", messageList);

    String scriptPath = "";
    try (Connection conn = tbDataSource.getConnection()) {
        scriptPath = "/dbscripts/"
                + StringUtils.deleteWhitespace(conn.getMetaData().getDatabaseProductName().toLowerCase())
                + "-createdb.sql";
        messageList.add("Running database script: " + scriptPath);
        ClassPathResource script = new ClassPathResource(scriptPath);
        ResourceDatabasePopulator populator = new ResourceDatabasePopulator(false, true, null, script);
        populator.populate(conn);
        messageList.add("Script ran without error");
        map.put("status", StartupStatus.needsBootstrapping);
    } catch (ScriptException | SQLException ex) {
        messageList.add("ERROR processing database script " + scriptPath);
        messageList.add(ex.getMessage());
        map.put("status", StartupStatus.databaseCreateError);
    }

    return new ModelAndView(".install", map);
}