org.springframework.jdbc.datasource.init.ScriptException Java Examples

The following examples show how to use org.springframework.jdbc.datasource.init.ScriptException. 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 File: Application.java    From Intro-to-Spring-Hadoop with Apache License 2.0 6 votes vote down vote up
@Bean
DataSourceInitializer hiveInitializer(final DataSource dataSource) {
	final String ddl = "create external table if not exists tweetdata (value STRING) LOCATION '" + input + "'";
	final DataSourceInitializer dsi = new DataSourceInitializer();
    dsi.setDataSource(dataSource);
	dsi.setDatabasePopulator(new DatabasePopulator() {
		@Override
		public void populate(Connection conn) throws SQLException,
				ScriptException {
			Statement st = conn.createStatement();
			st.execute(ddl);
			st.close();
		}
	});
	return dsi;
}
 
Example #2
Source File: DynamicResourceDatabasePopulator.java    From multitenant with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 * @see #execute(DataSource)
 */
@Override
public void populate(Connection connection) throws ScriptException {
	Assert.notNull(connection, "Connection must not be null");
	for (Resource script : this.scripts) {
		EncodedResource encodedScript = new EncodedResource(script, this.sqlScriptEncoding);
		ScriptUtils.executeSqlScript(connection, encodedScript, this.continueOnError, this.ignoreFailedDrops,
				this.commentPrefix, this.separator, this.blockCommentStartDelimiter, this.blockCommentEndDelimiter, vars);
	}
}
 
Example #3
Source File: BatchMetricsFlatFileToDbIntegrationTest.java    From spring-boot-starter-batch-web with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws ScriptException {
	jdbcTemplate = new JdbcTemplate(dataSource);
	try {
		ScriptUtils.executeSqlScript(dataSource.getConnection(),
				new ClassPathResource("metrics/create-schema.sql"));
	} catch (Exception e) {
		// if table exist, error is okay.
	}
}
 
Example #4
Source File: DynamicResourceDatabasePopulator.java    From multitenant with Apache License 2.0 2 votes vote down vote up
/**
 * Execute this {@code ResourceDatabasePopulator} against the given
 * {@link DataSource}.
 * <p>Delegates to {@link DatabasePopulatorUtils#execute}.
 * @param dataSource the {@code DataSource} to execute against (never {@code null})
 * @throws ScriptException if an error occurs
 * @since 4.1
 * @see #populate(Connection)
 */
public void execute(DataSource dataSource) throws ScriptException {
	DatabasePopulatorUtils.execute(this, dataSource);
}
 
Example #5
Source File: ScriptUtils.java    From multitenant with Apache License 2.0 2 votes vote down vote up
/**
 * Split an SQL script into separate statements delimited by the provided
 * separator character. Each individual statement will be added to the
 * provided {@code List}.
 * <p>Within the script, {@value #DEFAULT_COMMENT_PREFIX} will be used as the
 * comment prefix; any text beginning with the comment prefix and extending to
 * the end of the line will be omitted from the output. Similarly,
 * {@value #DEFAULT_BLOCK_COMMENT_START_DELIMITER} and
 * {@value #DEFAULT_BLOCK_COMMENT_END_DELIMITER} will be used as the
 * <em>start</em> and <em>end</em> block comment delimiters: any text enclosed
 * in a block comment will be omitted from the output. In addition, multiple
 * adjacent whitespace characters will be collapsed into a single space.
 * @param script the SQL script
 * @param separator character separating each statement &mdash; typically a ';'
 * @param statements the list that will contain the individual statements
 * @throws ScriptException if an error occurred while splitting the SQL script
 * @see #splitSqlScript(String, String, List)
 * @see #splitSqlScript(EncodedResource, String, String, String, String, String, List)
 */
public static void splitSqlScript(String script, char separator, List<String> statements) throws ScriptException {
	splitSqlScript(script, String.valueOf(separator), statements);
}
 
Example #6
Source File: ScriptUtils.java    From multitenant with Apache License 2.0 2 votes vote down vote up
/**
 * Split an SQL script into separate statements delimited by the provided
 * separator string. Each individual statement will be added to the
 * provided {@code List}.
 * <p>Within the script, {@value #DEFAULT_COMMENT_PREFIX} will be used as the
 * comment prefix; any text beginning with the comment prefix and extending to
 * the end of the line will be omitted from the output. Similarly,
 * {@value #DEFAULT_BLOCK_COMMENT_START_DELIMITER} and
 * {@value #DEFAULT_BLOCK_COMMENT_END_DELIMITER} will be used as the
 * <em>start</em> and <em>end</em> block comment delimiters: any text enclosed
 * in a block comment will be omitted from the output. In addition, multiple
 * adjacent whitespace characters will be collapsed into a single space.
 * @param script the SQL script
 * @param separator text separating each statement &mdash; typically a ';' or newline character
 * @param statements the list that will contain the individual statements
 * @throws ScriptException if an error occurred while splitting the SQL script
 * @see #splitSqlScript(String, char, List)
 * @see #splitSqlScript(EncodedResource, String, String, String, String, String, List)
 */
public static void splitSqlScript(String script, String separator, List<String> statements) throws ScriptException {
	splitSqlScript(null, script, separator, DEFAULT_COMMENT_PREFIX, DEFAULT_BLOCK_COMMENT_START_DELIMITER,
			DEFAULT_BLOCK_COMMENT_END_DELIMITER, statements);
}
 
Example #7
Source File: ScriptUtils.java    From multitenant with Apache License 2.0 2 votes vote down vote up
/**
 * Execute the given SQL script using default settings for statement
 * separators, comment delimiters, and exception handling flags.
 * <p>Statement separators and comments will be removed before executing
 * individual statements within the supplied script.
 * <p><strong>Warning</strong>: this method does <em>not</em> release the
 * provided {@link Connection}.
 * @param connection the JDBC connection to use to execute the script; already
 * configured and ready to use
 * @param resource the resource to load the SQL script from; encoded with the
 * current platform's default encoding
 * @throws ScriptException if an error occurred while executing the SQL script
 * @see #DEFAULT_STATEMENT_SEPARATOR
 * @see #DEFAULT_COMMENT_PREFIX
 * @see #DEFAULT_BLOCK_COMMENT_START_DELIMITER
 * @see #DEFAULT_BLOCK_COMMENT_END_DELIMITER
 * @see org.springframework.jdbc.datasource.DataSourceUtils#getConnection
 * @see org.springframework.jdbc.datasource.DataSourceUtils#releaseConnection
 */
public static void executeSqlScript(Connection connection, Resource resource) throws ScriptException {
	executeSqlScript(connection, new EncodedResource(resource));
}
 
Example #8
Source File: ScriptUtils.java    From multitenant with Apache License 2.0 2 votes vote down vote up
/**
 * Execute the given SQL script using default settings for statement
 * separators, comment delimiters, and exception handling flags.
 * <p>Statement separators and comments will be removed before executing
 * individual statements within the supplied script.
 * <p><strong>Warning</strong>: this method does <em>not</em> release the
 * provided {@link Connection}.
 * @param connection the JDBC connection to use to execute the script; already
 * configured and ready to use
 * @param resource the resource (potentially associated with a specific encoding)
 * to load the SQL script from
 * @throws ScriptException if an error occurred while executing the SQL script
 * @see #DEFAULT_STATEMENT_SEPARATOR
 * @see #DEFAULT_COMMENT_PREFIX
 * @see #DEFAULT_BLOCK_COMMENT_START_DELIMITER
 * @see #DEFAULT_BLOCK_COMMENT_END_DELIMITER
 * @see org.springframework.jdbc.datasource.DataSourceUtils#getConnection
 * @see org.springframework.jdbc.datasource.DataSourceUtils#releaseConnection
 */
public static void executeSqlScript(Connection connection, EncodedResource resource) throws ScriptException {
	executeSqlScript(connection, resource, false, false, DEFAULT_COMMENT_PREFIX, DEFAULT_STATEMENT_SEPARATOR,
			DEFAULT_BLOCK_COMMENT_START_DELIMITER, DEFAULT_BLOCK_COMMENT_END_DELIMITER, null);
}