问题: java 执行mysql文件的时候,程序显示执行成功,但是数据库却没有数据
private void executeSQL(String sqlName) {
Resource resource = new ClassPathResource("sql/base/"+ sqlName);
if (!resource.exists()) {
return;
}
try (InputStream inputStream = resource.getInputStream(); InputStreamReader reader = new InputStreamReader(inputStream, "utf-8")) {
Connection connection = abstractDataSource.getConnection();
ScriptRunner scriptRunner = new ScriptRunner(connection);
scriptRunner.setLogWriter(null);
scriptRunner.setErrorLogWriter(null);
scriptRunner.setSendFullScript(true);// true执行所有代码,否则按行识别代码
scriptRunner.setAutoCommit(true);
scriptRunner.runScript(reader);
} catch (IOException | SQLException e) {
log.error("读取初始化sql脚本异常", e);
throw new DefaultException("读取初始化sql脚本异常");
}
}
解决:需要设置连接支持批量处理
jdbc:mysql://{host}:{port}/{dbName}?useUnicode=true&characterEncoding=UTF8&useSSL=false&serverTimezone=Asia/Shanghai&allowMultiQueries=true
加上 &allowMultiQueries=true
即可