zoukankan      html  css  js  c++  java
  • java 执行sql文件

    # 背景

    用例执行完毕,期望回滚数据,因此希望执行sql来回滚数据

    # 步骤

    直接show代码,借助的是mybatis的ScriptRunner

    /**
         * 执行xx库下的表备份脚本
         *
         * @param tableName
         */
        public static void runSqlInStat(String tableName) {
    
            String className = Configurations.INSTANCE.get("jdbc.xx.driver");
            String dbUrl = Configurations.INSTANCE.get("jdbc.xx.url");
            String dbUsername = Configurations.INSTANCE.get("jdbc.xx.username");
            String dbPassword = Configurations.INSTANCE.get("jdbc.xx.password");
    
            try {
                Class.forName(className);
                Connection conn = DriverManager.getConnection(dbUrl, dbUsername, dbPassword);
                ScriptRunner runner = new ScriptRunner(conn);
                runner.setAutoCommit(true);
    
                String fileName = String.format("src/main/resources/db/%s.sql", tableName);
                File file = new File(fileName);
    
                try {
                    if (file.getName().endsWith(".sql")) {
                        runner.setFullLineDelimiter(false);
                        runner.setDelimiter(";");//语句结束符号设置
                        runner.setLogWriter(null);//日志数据输出,这样就不会输出过程
                        runner.setSendFullScript(false);
                        runner.setAutoCommit(true);
                        runner.setStopOnError(true);
                        runner.runScript(new InputStreamReader(new FileInputStream(fileName), "utf8"));
                        logger.info(String.format("【%s】回滚成功", tableName));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
    
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
    
                e.printStackTrace();
            }
    
        }
  • 相关阅读:
    更改Delphi系统的默认字体
    Delphi TThread中文注释
    Delphi中的线程类 TThread详解
    TreeView使用笔记
    用未公开函数实现Shell操作监视
    Delphi面向对象编程的20条规则
    Delphi操作Excel命令
    delphi 创建一个纯文本文件
    判断滚动条到底部、
    数据库性能优化之SQL语句优化1
  • 原文地址:https://www.cnblogs.com/jwentest/p/8761357.html
Copyright © 2011-2022 走看看