Java執(zhí)行SQL腳本文件到數(shù)據(jù)庫詳解
本文實例為大家分享了Java執(zhí)行SQL腳本文件到數(shù)據(jù)庫的具體方式,供大家參考,具體內(nèi)容如下
方式一:直接讀取SQL腳本文件的內(nèi)容,然后傳遞到SQL中。
代碼:RunSqlService:
@Autowired
private RunSqlDao runSqlDao;
/**
* 讀取文件內(nèi)容到SQL中執(zhí)行
* @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為文件結(jié)束
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>
這種寫法:只支持?jǐn)?shù)據(jù)的變化(新增、修改、刪除),且SQL文件內(nèi)容以begin開始,以end結(jié)束。無法更新表字段修改等操作。
方式二;使用ScriptRunner
代碼:RunSqlService:
/**
* 執(zhí)行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,即一次執(zhí)行一個SQL語句,否則就會報錯。
方式三:使用ScriptUtils
代碼:RunSqlService:(以下兩種方式:腳本.Sql 和RunSqlService 在同一目錄下)
方法(1)
/**
* 執(zhí)行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)
/**
* 執(zhí)行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的,否則執(zhí)行到數(shù)據(jù)中漢字是亂碼。
方法(2)解決了方法(1)的問題,完美了,喜歡的小伙伴們快拿去享用吧。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java基礎(chǔ)之Comparable與Comparator概述
這篇文章主要介紹了Java基礎(chǔ)之Comparable與Comparator詳解,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)java基礎(chǔ)的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04
SpringBoot 使用 Ehcache 作為緩存的操作方法
這篇文章主要介紹了SpringBoot 如何使用 Ehcache 作為緩存,我們通過添加 Ehcache 依賴、創(chuàng)建 Ehcache 配置文件并在 Spring Boot 應(yīng)用程序的配置文件中啟用 Ehcache 緩存,來配置 Ehcache 緩存,需要的朋友可以參考下2023-06-06
Sentinel結(jié)合Nacos實現(xiàn)數(shù)據(jù)持久化過程詳解
這篇文章主要介紹了Sentinel結(jié)合Nacos實現(xiàn)數(shù)據(jù)持久化過程,要持久化的原因是因為每次啟動Sentinel都會使之前配置的規(guī)則就清空了,這樣每次都要再去設(shè)定規(guī)則顯得非常的麻煩,感興趣想要詳細(xì)了解可以參考下文2023-05-05
spring?boot只需兩步優(yōu)雅整合activiti示例解析
這篇文章主要主要來教大家spring?boot優(yōu)雅整合activiti只需兩步就可完成測操作示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助祝大家多多進(jìn)步2022-03-03
線程池ThreadPoolExecutor并行處理實現(xiàn)代碼
這篇文章主要介紹了線程池ThreadPoolExecutor并行處理實現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-11-11
在Java中將double轉(zhuǎn)換為int的操作方法
這篇文章主要介紹了在Java中將double轉(zhuǎn)換為int的操作方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03
使用Spring Cache和Redis實現(xiàn)查詢數(shù)據(jù)緩存
在現(xiàn)代應(yīng)用程序中,查詢緩存的使用已經(jīng)變得越來越普遍,它不僅能夠顯著提高系統(tǒng)的性能,還能提升用戶體驗,在這篇文章中,我們將探討緩存的基本概念、重要性以及如何使用Spring Cache和Redis實現(xiàn)查詢數(shù)據(jù)緩存,需要的朋友可以參考下2024-07-07

