SpringBoot整合jdbcTemplate执行SQL脚本文件

SpringBoot整合jdbcTemplate执行SQL脚本文件

		DriverManagerDataSource dataSource=new DriverManagerDataSource();
		dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");//连接mysql
		dataSource.setUrl("jdbc:mysql://ip:3306");
		dataSource.setUsername("root");
		dataSource.setPassword("root");
		JdbcTemplate jdbcTemplate=new JdbcTemplate(dataSource);
		Map<String, Object> map = jdbcTemplate.queryForMap("");
		System.out.println(map);
		int update = jdbcTemplate.update("");
		System.out.println(update);
		File file=new File("xxx.sql");
		FileReader fileReader=new FileReader(file);
		BufferedReader bf=new BufferedReader(fileReader);
		StringBuffer sb=new StringBuffer();
		String line;
		while ((line = bf.readLine()) != null) {
			sb.append(line);
			sb.append("\n");
		}
		System.out.println(sb);

		jdbcTemplate.execute(sb.toString());
		Connection connection = jdbcTemplate.getDataSource().getConnection();
		ScriptRunner runner = new ScriptRunner(connection);
		runner.setAutoCommit(true);
		File file = new File("xxxx.sql");
		if (file.getName().endsWith(".sql")) {
			runner.setFullLineDelimiter(false);
			runner.setDelimiter(";");//语句结束符号设置
			runner.setLogWriter(null);//日志数据输出,这样就不会输出过程
			runner.setSendFullScript(false);
			runner.setAutoCommit(true);
			runner.setStopOnError(true);
			runner.runScript(new InputStreamReader(new FileInputStream("xxx.sql"), "utf8"));
		}

你可能感兴趣的:(后端,sql,spring,boot,数据库)