mysql(31) : java导入sql文件

入口


import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;

/**
 * @Author: liyue
 * @Date: 2022/04/18/16:05
 * @Description:
 */
public class Init {

    public static void main(String[] args) {
        if (args == null || args.length != 6) {
            System.err.println("参数数量错误,必须为6个,当前为[" + args.length + "]个");
            System.out.println("参数说明:");
            System.out.println("    参数1:mysql的ip");
            System.out.println("    参数2:mysql的端口");
            System.out.println("    参数3:mysql的账号名");
            System.out.println("    参数4:mysql的密码");
            System.out.println("    参数5:sql文件夹绝对路径");
            System.out.println("    参数6:导入的数据库名");
            return;
        }
        System.out.println("准备导入sql,参数信息如下:");
        for (int i = 0; i < args.length; i++) {
            System.out.println("参数[" + (i + 1) + "]:" + args[i]);
        }

        System.out.println("sql文件夹路径为:" + args[4]);
        File file = new File(args[4]);
        if (!file.exists()) {
            System.err.println("sql文件夹路径为空,数据库初始化逻辑终止");
            return;
        }
        File[] files = file.listFiles();
        if (files.length == 0) {
            System.err.println("sql文件夹内文件数量为空,数据库初始化逻辑终止");
            return;
        }
        StringBuffer sb = new StringBuffer();
        System.out.println();
        for (File f : files) {
            String path = f.getPath();
            System.out.println("发现待导入sql文件:" + path);
            sb.append(path).append("\n");
        }
        System.out.println();
        sb.delete(sb.length() - 1, sb.length());
        args[4] = sb.toString();

        Mysql8Instance mysql = new Mysql8Instance(args[0], Integer.parseInt(args[1]), "mysql", args[2], args[3]);
        String database = args[5];
        List> query = mysql.query("show databases;");
        for (List objects : query) {
            if (objects.contains(database)) {
                System.out.println("存在[" + database + "]数据,数据库初始化逻辑终止");
                return;
            }
        }
        System.out.println("数据库[" + database + "]不存在,创建该数据库");
        mysql.execute(" CREATE DATABASE  IF NOT EXISTS `" + database + "`  DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;");
        System.out.println("数据库[" + database + "]创建成功");
        mysql.close();

        source(args);
    }

    public static void source(String[] args) {
        String className = "com.mysql.cj.jdbc.Driver";
        String dbUrl = "jdbc:mysql://" + args[0] + ":" + args[1] + "/" + args[5] + "?useUnicode=true&useSSL=false&characterEncoding=utf-8&serverTimezone=GMT%2b8&allowMultiQueries=true";
        String dbUsername = args[2];
        String dbPassword = args[3];
        try {
            Class.forName(className);
            Connection conn = DriverManager.getConnection(dbUrl, dbUsername, dbPassword);
            ScriptRunner runner = new ScriptRunner(conn);
            runner.setAutoCommit(true);
            try {
                runner.setFullLineDelimiter(false);
                //语句结束符号设置
                runner.setDelimiter(";");
                //日志数据输出,这样就不会输出过程
                runner.setLogWriter(null);
                runner.setSendFullScript(false);
                runner.setAutoCommit(true);
                runner.setStopOnError(true);
                for (String s : args[4].split("\n")) {
                    if (s.endsWith(".sql")) {
                        System.out.println("开始导入sql文件:[" + s + "]");
                        runner.runScript(new InputStreamReader(new FileInputStream(s), "utf8"));
                        System.out.println("导入sql文件:[" + s + "]成功");
                    } else {
                        System.err.println("文件[" + s + "]不是sql文件!");
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }
}
 
  

pom.xml



    4.0.0

    org.example
    mysql-init
    1.0-SNAPSHOT

    
        8
        8
    

    
        
            mysql
            mysql-connector-java
            8.0.18
        
        
            org.mybatis
            mybatis
            3.5.2
            compile
        
    

    
        
            org.springframework.boot
            spring-boot-maven-plugin
            
                
                cn.nordrassil.mysql.init.Init
                ZIP
                true
            
            
                
                    
                        repackage
                    
                
            
        
    
Mysql8Instance:mysql(30) : java管理mysql8(开发用轻量版)_Lxinccode的博客-CSDN博客

你可能感兴趣的:(mysql,java导入sql)