Java执行SQL脚本文件到数据库详解
作者:夏夏_夏七七 时间:2023-08-08 08:30:00
本文实例为大家分享了Java执行SQL脚本文件到数据库的具体方式,供大家参考,具体内容如下
方式一:直接读取SQL脚本文件的内容,然后传递到SQL中。
代码:RunSqlService:
@Autowired
private RunSqlDao runSqlDao;
/**
* 读取文件内容到SQL中执行
* @param sqlPath SQL文件的路径:如:D:/TestProject/web/sql/脚本.Sql
*/
public void runSqlByReadFileContent(String sqlPath) throws Exception {
try {
String sqlStr = readFileByLines(sqlPath);
// System.out.println("获得的文本:" + sqlStr);
if (sqlStr.length() > 0) {
runSqlDao.runSqlBySqlStr(sqlStr);
}
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
/**
* 以行为单位读取文件,常用于读面向行的格式化文件
*/
private String readFileByLines(String filePath) throws Exception {
StringBuffer str = new StringBuffer();
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(
new FileInputStream(filePath), "UTF-8"));
String tempString = null;
int line = 1;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
// 显示行号
// System.out.println("line " + line + ": " + tempString);
str = str.append(" " + tempString);
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
throw e;
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
return str.toString();
}
RunSqlDao :
/**
* @param sqlStr
*/
public void runSqlBySqlStr(String sqlStr) {
Map<String,Object> map=new HashMap<String,Object>();
map.put("sqlStr", sqlStr);
sqlSessionTemplate.selectList("runSql.runSqlBySqlStr", map);
}
SQLMap:
<mapper namespace="runSql">
<select id="runSqlBySqlStr" parameterType="map">
<![CDATA[ ${sqlStr}]]>
</select>
</mapper>
这种写法:只支持数据的变化(新增、修改、删除),且SQL文件内容以begin开始,以end结束。无法更新表字段修改等操作。
方式二;使用ScriptRunner
代码:RunSqlService:
/**
* 执行sql脚本文件 使用ScriptRunner
* @param sqlPath SQL文件的路径:如:D:/TestProject/web/sql/脚本.Sql
*/
public void runSqlByScriptRunner(String sqlPath) throws Exception {
try {
SqlSession sqlSession = sqlSessionFactory.openSession();
Connection conn = sqlSession.getConnection();
ScriptRunner runner = new ScriptRunner(conn);
runner.setEscapeProcessing(false);
runner.setSendFullScript(true);
runner.runScript(new InputStreamReader(new FileInputStream(sqlPath), "UTF-8"));
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
这种写法:只能有一行SQL,即一次执行一个SQL语句,否则就会报错。
方式三:使用ScriptUtils
代码:RunSqlService:(以下两种方式:脚本.Sql 和RunSqlService 在同一目录下)
方法(1)
/**
* 执行sql脚本文件 使用Spring工具类
*/
public void runSqlBySpringUtils() throws Exception {
try {
SqlSession sqlSession = sqlSessionFactory.openSession();
Connection conn = sqlSession.getConnection();
ClassPathResource rc = new ClassPathResource("脚本.Sql", RunSqlDao.class);
ScriptUtils.executeSqlScript(conn, rc);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
方法(2)
/**
* 执行sql脚本文件 使用Spring工具类
*/
public void runSqlBySpringUtils() throws Exception {
try {
SqlSession sqlSession = sqlSessionFactory.openSession();
Connection conn = sqlSession.getConnection();
ClassPathResource rc = new ClassPathResource("脚本.Sql", RunSqlDao.class);
EncodedResource er = new EncodedResource(rc, "utf-8");
ScriptUtils.executeSqlScript(conn, er);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
方法(1),脚本.Sql文件必须是ANSI的,否则执行到数据中汉字是乱码。
方法(2)解决了方法(1)的问题,完美了,喜欢的小伙伴们快拿去享用吧。
标签:Java,SQL,数据库
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
springboot+chatgpt+chatUI Pro开发智能聊天工具的实践
2023-10-30 05:38:03
![](https://img.aspxhome.com/file/2023/2/68012_0s.jpg)
一个JAVA小项目--Web应用自动生成Word
2022-04-30 07:19:56
![](https://img.aspxhome.com/file/2023/4/69134_0s.jpg)
opencv实现读取视频保存视频
2021-08-16 15:57:50
mybatis自定义类型处理器TypehHandler示例详解
2023-10-11 04:30:40
![](https://img.aspxhome.com/file/2023/3/69133_0s.png)
浅析JDK和Tomcat的安装与配置方法
2022-02-02 07:49:43
基于C#技术实现身份证识别功能
2023-10-01 14:16:26
![](https://img.aspxhome.com/file/2023/8/91358_0s.png)
详解使用JRebel插件实现SpringBoot应用代码热加载
2021-06-15 10:39:00
![](https://img.aspxhome.com/file/2023/5/85835_0s.jpg)
Java super关键字的使用详解
2021-11-30 13:40:43
![](https://img.aspxhome.com/file/2023/0/60780_0s.jpg)
java文件操作工具类分享(file文件工具类)
2023-11-24 22:32:47
解析如何在android中增加gsensor驱动(MMA7660)
2023-05-22 22:03:24
Java中if...else语句使用的学习教程
2023-10-08 03:30:59
c#获取相同概率随机数的算法代码
2022-09-07 21:18:13
Java 8中日期和时间的处理方法
2021-09-07 15:37:58
SpringBoot基于Sentinel在服务上实现接口限流
2023-11-27 17:19:25
![](https://img.aspxhome.com/file/2023/4/59574_0s.jpg)
C#实现最完整的文件和目录操作类实例
2022-05-19 09:38:51
Android studio 快捷键大全
2022-03-27 14:58:56
![](https://img.aspxhome.com/file/2023/4/116024_0s.png)
C# 手动/自动保存图片的实例代码
2023-08-06 09:36:35
浅谈java的byte数组的不同写法
2023-03-10 07:53:12
JPA @Basic单表查询如何实现大字段懒加载
2021-06-03 18:02:00
![](https://img.aspxhome.com/file/2023/7/101977_0s.png)
Java多个版本切换的几种方法
2022-04-22 14:10:15
![](https://img.aspxhome.com/file/2023/8/63108_0s.png)