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

Following code examples demonstrate how to use org.springframework.jdbc.datasource.init.ScriptExceptionfrom spring-framework. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.springframework.jdbc.datasource.init.ScriptExceptionand various code implementation of this class.

	public void deleteAllDBRecords(){
		try {
			ScriptUtils.executeSqlScript(jdbcTemplate.getDataSource().getConnection(),
					new FileSystemResource(cleanUpScript));
		} catch (ScriptException | SQLException e) {
			e.printStackTrace();
		}
	} 


	public static void executeSql(DataSource dataSource, String... sqlPaths) {
		if (sqlPaths == null || sqlPaths.length == 0) {
			return;
		}

		try {
			for (String sqlPath : sqlPaths) {
				final Resource resource = RESOURCE_LOADER.getResource(sqlPath);
				ScriptUtils.executeSqlScript(dataSource.getConnection(),
						new EncodedResource(resource, DEFAULT_ENCODING));
			}
		} catch (ScriptException | SQLException e) {
			throw Exceptions.unchecked(e);
		}
	} 

    public void setUp() {
        Resource resource = new ClassPathResource("sql/cleanup.sql", getClass().getClassLoader());
        Transaction tx = HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession().beginTransaction();
        HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession().doWork((Connection connection) -> {
            try {
                ScriptUtils.executeSqlScript(connection, resource);
            } catch (ScriptException e) {
                e.printStackTrace();
            }
        });
        tx.commit();
    } 

	public void load() {
		try {
			ScriptUtils.executeSqlScript(getConnection(), new ClassPathResource("db/drop.sql"));
		} catch (ScriptException e) {
		}
		ScriptUtils.executeSqlScript(getConnection(), new ClassPathResource("db/create.sql"));
		ScriptUtils.executeSqlScript(getConnection(), new ClassPathResource("db/create.sqlite3.sql"));
	} 

Advertisement
Javadoc
Root of the hierarchy of data access exceptions that are related to processing of SQL scripts. @auth

or Sam Brannen @since 4.0.3

Read More
Advertisement