Java Code Examples for org.springframework.jdbc.datasource.init.ScriptUtils#executeSqlScript()

The following examples show how to use org.springframework.jdbc.datasource.init.ScriptUtils#executeSqlScript() . 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: InitDatabaseConnection.java    From wind-im with Apache License 2.0 6 votes vote down vote up
private static void initDatabaseTable(Connection conn) throws SQLException {
    try {
        // 生成临时sql文件加载数据库sql执行脚本,
        File sqlFile = new File(WINDCHAT_MYSQL_SQL);
        if (!sqlFile.exists()) {
            FileUtils.writeResourceToFile("/" + WINDCHAT_MYSQL_SQL, sqlFile);
        }

        // 初始化数据库表
        File file = new File(WINDCHAT_MYSQL_SQL);
        if (!file.exists()) {
            throw new FileNotFoundException("init mysql with sql script file is not exists");
        }

        FileSystemResource rc = new FileSystemResource(file);
        EncodedResource encodeRes = new EncodedResource(rc, "GBK");
        ScriptUtils.executeSqlScript(conn, encodeRes);
        SqlLog.info("windchat init mysql database with sql-script finish");

        file.delete();
    } catch (Exception e) {
        throw new SQLException(e);
    }
}
 
Example 2
Source File: SQLiteUpgrade.java    From wind-im with Apache License 2.0 6 votes vote down vote up
/**
 * 通过sql脚本初始化数据库表
 *
 * @param conn
 * @throws SQLException
 */
private static void doInitWork(Connection conn) throws SQLException {
    try {
        // 生成临时sql文件加载数据库sql执行脚本,
        File sqlFile = new File(WINDCHAT_SQLITE_SQL);
        if (!sqlFile.exists()) {
            FileUtils.writeResourceToFile("/" + WINDCHAT_SQLITE_SQL, sqlFile);
        }

        // 初始化数据库表
        File file = new File(WINDCHAT_SQLITE_SQL);
        if (!file.exists()) {
            throw new FileNotFoundException("init mysql with sql script file is not exists");
        }

        FileSystemResource rc = new FileSystemResource(file);
        EncodedResource encodeRes = new EncodedResource(rc, "GBK");
        ScriptUtils.executeSqlScript(conn, encodeRes);
        SqlLog.info("windchat init sqlite with sql-script finish");

        file.delete();
    } catch (Exception e) {
        throw new SQLException(e);
    }
}
 
Example 3
Source File: InitDatabaseConnection.java    From openzaly with Apache License 2.0 6 votes vote down vote up
private static void initDatabaseTable(Connection conn) throws SQLException {
	try {
		// 生成临时sql文件加载数据库sql执行脚本,
		File sqlFile = new File(OPENZALY_MYSQL_SQL);
		if (!sqlFile.exists()) {
			FileUtils.writeResourceToFile("/" + OPENZALY_MYSQL_SQL, sqlFile);
		}

		// 初始化数据库表
		File file = new File(OPENZALY_MYSQL_SQL);
		if (!file.exists()) {
			throw new FileNotFoundException("init mysql with sql script file is not exists");
		}

		FileSystemResource rc = new FileSystemResource(file);
		EncodedResource encodeRes = new EncodedResource(rc, "GBK");
		ScriptUtils.executeSqlScript(conn, encodeRes);
		SqlLog.info("openzaly init mysql database with sql-script finish");

		file.delete();
	} catch (Exception e) {
		throw new SQLException(e);
	}
}
 
Example 4
Source File: SQLiteUpgrade.java    From openzaly with Apache License 2.0 6 votes vote down vote up
/**
 * 通过sql脚本初始化数据库表
 * 
 * @param conn
 * @throws SQLException
 */
private static void doInitWork(Connection conn) throws SQLException {
	try {
		// 生成临时sql文件加载数据库sql执行脚本,
		File sqlFile = new File(OPENZALY_SQLITE_SQL);
		if (!sqlFile.exists()) {
			FileUtils.writeResourceToFile("/" + OPENZALY_SQLITE_SQL, sqlFile);
		}

		// 初始化数据库表
		File file = new File(OPENZALY_SQLITE_SQL);
		if (!file.exists()) {
			throw new FileNotFoundException("init mysql with sql script file is not exists");
		}

		FileSystemResource rc = new FileSystemResource(file);
		EncodedResource encodeRes = new EncodedResource(rc, "GBK");
		ScriptUtils.executeSqlScript(conn, encodeRes);
		SqlLog.info("openzaly init sqlite with sql-script finish");

		file.delete();
	} catch (Exception e) {
		throw new SQLException(e);
	}
}
 
Example 5
Source File: InitDatabaseConnection.java    From openzaly with Apache License 2.0 6 votes vote down vote up
private static void initDatabaseTable(Connection conn) throws SQLException {
	try {
		// 生成临时sql文件加载数据库sql执行脚本,
		File sqlFile = new File(OPENZALY_MYSQL_SQL);
		if (!sqlFile.exists()) {
			FileUtils.writeResourceToFile("/" + OPENZALY_MYSQL_SQL, sqlFile);
		}

		// 初始化数据库表
		File file = new File(OPENZALY_MYSQL_SQL);
		if (!file.exists()) {
			throw new FileNotFoundException("init mysql with sql script file is not exists");
		}

		FileSystemResource rc = new FileSystemResource(file);
		EncodedResource encodeRes = new EncodedResource(rc, "GBK");
		ScriptUtils.executeSqlScript(conn, encodeRes);
		SqlLog.info("openzaly init mysql database with sql-script finish");

		file.delete();
	} catch (Exception e) {
		throw new SQLException(e);
	}
}
 
Example 6
Source File: IntegrationTestBase.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Before
public void before() throws Exception
{
    bindSession();
    executeStartupRoutines();

    IntegrationTestData annotation = this.getClass().getAnnotation( IntegrationTestData.class );

    if ( annotation != null && !dataInit )
    {
        ScriptUtils.executeSqlScript( jdbcTemplate.getDataSource().getConnection(),
            new EncodedResource( new ClassPathResource( annotation.path() ), StandardCharsets.UTF_8 ) );
        // only executes once per Unit Test
        dataInit = true;
    }

    // method that can be overridden by subclasses
    setUpTest();
}
 
Example 7
Source File: GeneratorController.java    From sc-generator with Apache License 2.0 5 votes vote down vote up
@RequestMapping(value = "/executeSql", method = RequestMethod.POST)
public void executeSql(MultipartFile file, HttpServletResponse response) throws SQLException, IOException {
    Connection connection = DriverManager.getConnection(url + "mysql", username, password);
    String db = ("SC" + UUID.randomUUID().toString().substring(0, 5)).toLowerCase();
    List<String> sqls = Arrays.asList("CREATE DATABASE IF NOT EXISTS " + db + " DEFAULT CHARSET utf8",
            "CREATE USER '" + db + "'@'%' IDENTIFIED BY '" + db + "'",
            "grant all privileges on " + db + ".* to '" + db + "'@'%' identified by '" + db + "'");
    for (String sql : sqls) {
        Statement statement = connection.createStatement();
        statement.execute(sql);
        statement.close();
    }

    connection.close();

    String jdbcUrl = url + db;
    connection = DriverManager.getConnection(url, db, db);
    ScriptUtils.executeSqlScript(connection, new InputStreamResource(file.getInputStream()));
    connection.close();

    datasourceService.save(new Datasource()
            .setJdbcUrl(jdbcUrl)
            .setName(file.getOriginalFilename().substring(0, file.getOriginalFilename().lastIndexOf(".")))
            .setUsername(db)
            .setPassword(db)
            .setDriver("com.mysql.jdbc.Driver"));
    response.sendRedirect("/");
}
 
Example 8
Source File: InitDatabase.java    From camelinaction2 with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    String url = "jdbc:postgresql://localhost:5432/quartz";

    Class.forName("org.postgresql.Driver");
    Connection db = DriverManager.getConnection(url, "quartz", "quartz");

    System.out.println("Initializing database and creating tables");
    db.setAutoCommit(false);
    ScriptUtils.executeSqlScript(db, new FileSystemResource("tables_postgres.sql"));
    db.setAutoCommit(true);

    db.close();

    System.out.println("Database initialized");
}
 
Example 9
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 10
Source File: TestHelper.java    From tutorials with MIT License 5 votes vote down vote up
private void runScript(String scriptName, DataSource dataSouorce) throws SQLException {
    DefaultResourceLoader resourceLoader = new DefaultResourceLoader();
    Resource script = resourceLoader.getResource(scriptName);
    try (Connection con = dataSouorce.getConnection()) {
        ScriptUtils.executeSqlScript(con, script);
    }
}