Example usage for org.springframework.jdbc.datasource.init ScriptUtils executeSqlScript

List of usage examples for org.springframework.jdbc.datasource.init ScriptUtils executeSqlScript

Introduction

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

Prototype

public static void executeSqlScript(Connection connection, EncodedResource resource) throws ScriptException 

Source Link

Document

Execute the given SQL script using default settings for statement separators, comment delimiters, and exception handling flags.

Usage

From source file:com.app.util.DatabaseUtil.java

public static void initializeDatabase(String path) throws DatabaseConnectionException, SQLException {

    try {/*from   w  w w .j  a  v  a  2  s  .  c  om*/
        ReleaseUtil.getReleaseVersion(_APPLICATION_RELEASE_NAME);
    } catch (SQLException sqle) {
        Resource resource = new ClassPathResource(path);

        ScriptUtils.executeSqlScript(getDatabaseConnection(), resource);

        ReleaseUtil.addRelease(_APPLICATION_RELEASE_NAME, _APPLICATION_VERSION);
    }
}

From source file:com.app.test.BaseTestCase.java

protected static void setUpDatabase() throws Exception {
    String databasePassword = System.getProperty("jdbc.default.password");
    String databaseURL = System.getProperty("jdbc.default.url");
    String databaseUsername = System.getProperty("jdbc.default.username");

    DatabaseUtil.setDatabaseProperties(databaseURL, databaseUsername, databasePassword);

    Resource resource = new ClassPathResource(_TEST_DATABASE_PATH);

    ScriptUtils.executeSqlScript(DatabaseUtil.getDatabaseConnection(), resource);
}

From source file:com.github.dactiv.fear.service.test.remoting.HttpRemotingInvokeTest.java

/**
 * ?sql/*www.jav  a2 s  . co  m*/
 *
 * @param connection jdbc 
 * @param sqlResourcePaths sql 
 *
 * @throws java.sql.SQLException
 */
private static void executeScript(Connection connection, String... sqlResourcePaths) throws SQLException {

    for (String sqlResourcePath : sqlResourcePaths) {
        Resource resource = resourceLoader.getResource(sqlResourcePath);
        ScriptUtils.executeSqlScript(connection, resource);
    }

    connection.close();
}

From source file:com.github.dactiv.fear.commons.test.ServiceTestCaseSupport.java

/**
 * ?sql// w w  w .j  a v  a 2  s  . co  m
 *
 * @param connection jdbc 
 * @param sqlResourcePaths sql 
 *
 * @throws SQLException
 */
private void executeScript(Connection connection, String... sqlResourcePaths) throws SQLException {

    for (String sqlResourcePath : sqlResourcePaths) {
        Resource resource = resourceLoader.getResource(sqlResourcePath);
        ScriptUtils.executeSqlScript(connection, resource);
    }
    connection.close();
}

From source file:de.codecentric.batch.test.metrics.BatchMetricsFlatFileToDbIntegrationTest.java

@Before
public void setUp() throws ScriptException {
    jdbcTemplate = new JdbcTemplate(dataSource);
    try {//from  ww w.  ja  v  a 2 s . c o  m
        ScriptUtils.executeSqlScript(dataSource.getConnection(),
                new ClassPathResource("metrics/create-schema.sql"));
    } catch (Exception e) {
        // if table exist, error is okay.
    }
}

From source file:org.awesomeagile.dao.testing.TestDatabase.java

private void executeScript(Optional<String> databaseName, final String script) {
    JdbcTemplate jdbcTemplate = databaseName.isPresent() ? jdbcTemplate(databaseName.get()) : jdbcTemplate();
    jdbcTemplate.execute(new ConnectionCallback<Object>() {
        @Override/*from w  w  w.  j  a va2  s  .  co  m*/
        public Object doInConnection(Connection con) throws SQLException, DataAccessException {
            ScriptUtils.executeSqlScript(con, new ClassPathResource(script));
            return null;
        }
    });
}

From source file:fr.gael.dhus.server.http.webapp.symmetricDS.SymmetricDSWebapp.java

@Override
public void checkInstallation() throws Exception {
    if (!scalabilityManager.isMaster())
        return;/*from  ww  w.j  a  v  a 2s  . c  o m*/

    // Check database is ready for SymmetricDS
    PreparedStatement ps = datasource.getConnection().prepareStatement(
            "SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE CONSTRAINT_NAME='SYM_FK_TRGPLT_2_TR';",
            ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

    while (!ps.executeQuery().first()) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    ps.close();
    // Check if init.sql has already been executed
    ps = datasource.getConnection().prepareStatement(
            "SELECT node_group_id FROM SYM_NODE_GROUP WHERE node_group_id = 'dhus-replica-group';",
            ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

    if (!ps.executeQuery().first()) {
        // Wait for master group to be inserted         
        PreparedStatement ps2 = datasource.getConnection().prepareStatement(
                "SELECT * FROM SYM_NODE_GROUP WHERE node_group_id='dhus-master-group';",
                ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

        while (!ps2.executeQuery().first()) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        ps2.close();

        ScriptUtils.executeSqlScript(datasource.getConnection(),
                new ClassPathResource("fr/gael/dhus/server/http/webapp/symmetricDS/init.sql"));
        LOGGER.info("SymmetricDS initialization script loaded");

        // Force the synchronizers to be reloaded
        HttpClient httpclient = HttpClients.createDefault();
        HttpPost httppost = new HttpPost(
                cfgManager.getServerConfiguration().getLocalUrl() + "/sync/api/engine/synctriggers");

        httpclient.execute(httppost);
    }
    ps.close();
}