在sql文件中将内容拿出来 并且执行

背景: 之前用mysqldump 执行 是简单 直接一个语句就好了 但是很卡 特别卡 所以我优化了一下 备份还原的功能 以下功能为还原功能 10000条数据 大概一秒多 也还好了

拿sql文件内容方法

           private static List<String> loadSql(String sqlFile) throws Exception {
        List<String> sqlList = new ArrayList<String>();
        try {
            StringBuffer sqlSb = new StringBuffer();
            char[] buff = new char[1024];
            int byteRead = 0;
            FileInputStream sqlFileIn = new FileInputStream(sqlFile);
            InputStreamReader isr = new InputStreamReader(sqlFileIn, "GBK");
            BufferedReader in = new BufferedReader(isr);

            while ((byteRead = in.read(buff)) != -1) {
                sqlSb.append(new String(buff, 0, byteRead));
            }
            // Windows 下换行是 /r/n, Linux 下是 /n
            String[] sqlArr = sqlSb.toString().split("(;//s*//r//n)|(;//s*//n)|(;)");
            for (int i = 0; i < sqlArr.length; i++) {
                String sql = sqlArr[i].replaceAll("--.*", "").trim();
                if (!sql.equals("")) {
                    sqlList.add(sql);
                }
            }

            sqlFileIn.close();
            return sqlList;
        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
    }

执行sql 方法

    public static boolean restoreMySQL(String filePath) {
        log.info("restoreMySQL start");
        DataBaseMysqlAutoConfig config = SpringUtils.getBean(DataBaseMysqlAutoConfig.class);
        String username = config.dbProperties().getUsername();
        String password = config.dbProperties().getPassword();
        int port = config.dbProperties().getPort();
        String database = config.dbProperties().getDatabase();
        Connection connection = connectSQL("com.mysql.cj.jdbc.Driver", "jdbc:mysql://localhost:" + port + "/" + database + "?useUnicode=true&zeroDateTimeBehavior=convertToNull&autoReconnect=true&characterEncoding=utf-8&serverTimezone=UTC", username, password);//连接数据库
        try {
            connection.setAutoCommit(false);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (connection != null) {
            Statement stmt = null;
            List<String> sqlList = null;
            try {
                sqlList = loadSql(filePath);
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                stmt = connection.createStatement();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            for (String sql : sqlList) {
                try {
                    stmt.addBatch(sql);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            int[] rows = new int[0];
            try {
                rows = stmt.executeBatch();
                connection.commit(); // 提交
                connection.close();
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            log.info("restoreMySQL end  Row count==={}", rows.length);
            return true;
        } else {
            log.info("restoreMySQL conn fail");
            return false;
        }

    }

你可能感兴趣的:(sql,数据库)