zoukankan      html  css  js  c++  java
  • 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();
                }
            }
        }
    }
  • 相关阅读:
    MD代码块指定语言类型
    spring通过bean名称,方法名,反射调用服务。
    h5魔塔开坑记
    意识流CSP2021游记
    Android开发byte version = 0x80错误: 不兼容的类型: 从int转换到byte可能会有损失
    Installed Build Tools revision 31.0.0 is corrupted. Remove and install again using the SDK Manager解决方法
    Android开发androidstudio调试smali代码
    Android开发修改手机ro.debuggable=1便于调试应用程序
    window环境下载Android系统源代码的方法
    android开发jni开发遍历文件夹下的文件以及目录
  • 原文地址:https://www.cnblogs.com/fangyan1994/p/14123592.html
Copyright © 2011-2022 走看看