java執行SQL腳本文件


1. 在工作中很多時候需要執行一個SQL腳本文件到數據庫中作為初始化數據;spring提供了一個工具類ScriptUtils,具體用法如下:

@SpringBootTest
class ExecuteSqlScriptApplicationTests {
    @Autowired
    private DataSource dataSource;

    @Test
    void contextLoads() throws SQLException, IOException {
        Resource classPathResource = new ClassPathResource("init/student.sql");
        ScriptUtils.executeSqlScript(dataSource.getConnection(), classPathResource);

    }

}

2. 但是有時候我們的SQL腳本文件很大,甚至是幾百mb,這樣容易造成內存溢出的情況,因此我寫了一個工具類,對SQL腳本進行拆解,然后批量執行。  這樣每批量執行后,就清空緩存中的SQL,因此解決內存溢出問題。如下:

具體還沒有用大數據量的腳本測試,等周一到公司再測試一下吧,哈哈哈。。。

@SpringBootTest
class ExecuteSqlScriptApplicationTests {
    @Autowired
    private DataSource dataSource;

    @Test
    void contextLoads() throws SQLException, IOException {
        Resource classPathResource = new ClassPathResource("init/student.sql");
        ScriptUtils.executeSqlScript(dataSource.getConnection(), classPathResource);
        // 分批處理SQL腳本
        batchExecuteSql(classPathResource, 5);
    }

    /**
     * SQL腳本分解執行
     * @param resource SQL腳本資源
     * @param batchNumber  每多少條SQL執行一次
     * @throws SQLException
     * @throws IOException
     */
    public void batchExecuteSql(Resource resource, int batchNumber) throws SQLException, IOException {
        Connection connection = dataSource.getConnection();
        Statement statement = connection.createStatement();
        BufferedReader bufferedReader = null;
        try {
            //獲取字符緩沖流
            bufferedReader = new BufferedReader(new InputStreamReader(resource.getInputStream()));
            int l;
            int i = 0;
            StringBuilder sql = new StringBuilder();
            while ((l = bufferedReader.read()) != -1) {
                char read = (char) l;
                sql.append(read);
                if (read == ';') { // 一個完整的SQL語句
                    i ++;
                    statement.addBatch(sql.toString());
                    if (i % batchNumber == 0) {
                        System.out.println("每" + batchNumber + "條語句批量執行開始......");
                        statement.executeBatch();
                        statement.clearBatch();
                        System.out.println("每" + batchNumber + "條語句批量執行結束......");
                    }
                    //清除StringBuilder中的SQL語句
                    sql.delete(0, sql.length());
                }
            }
            if (i % batchNumber != 0) {
                System.out.println("執行最后不足" + batchNumber + "條語句開始!!!");
                statement.executeBatch();
                statement.clearBatch();
                System.out.println("執行最后不足" + batchNumber + "條語句結束!!!");
            }
        } finally {
            if (bufferedReader != null) {
                bufferedReader.close();
            }
            if (connection != null) {
                connection.close();
            }
            if (statement != null) {
                statement.close();
            }
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM