Java执行Mysql导入

最近要实现一个功能,上传SQL文件,后台执行上传SQL文件导入MYSQL数据库。实现方式:

1、读取上传文件,添加到Batch中批量执行。
String sql = "";
BufferedReader br = new BufferedReader(new FileReader(file));
                                       while ((sql = br.readLine()) != null) {//使用readLine方法,一次读一行
                        if (sql.length() > 1) {
                            if (isFirstLine) {
                                sql = sql.substring(1);
                            }
                            stmt.addBatch(sql);
                            executeCount++;
                        }
                        isFirstLine = false;
                    }
                    br.close();


缺点:必须一行是一个可执行SQL语句。


2、将上传SQL文件读取到BufferedReader中,调用mysql执行。
                        Process child = rt.exec("mysql -uroot -proot --default-character-set=utf8  prototype ");
                        OutputStream out = child.getOutputStream();//控制台的输入信息作为输出流
                        String line;
                        StringBuffer sb = new StringBuffer("");
                        String outStr;
                        BufferedReader br = new BufferedReader(new InputStreamReader(
                                new FileInputStream(fPath), "utf8"));
                        while ((line = br.readLine()) != null) {
                            sb.append(line + "\r\n");
                        }
                        outStr = sb.toString();
            
                        OutputStreamWriter writer = new OutputStreamWriter(out, "utf8");
                        writer.write(outStr);
                        // 注:这里如果用缓冲方式写入文件的话,会导致中文乱码,用flush()方法则可以避免
                        writer.flush();
                        // 别忘记关闭输入输出流
                        out.close();
                        br.close();
                        writer.close();


3、通过Runtime.exec方法,直接调用mysql导入功能导入。2部操作
String username = "root";//用户名  
                        String password = "root";//密码  
                        String host = "localhost";//导入的目标数据库所在的主机  
                        String port = "3306";//使用的端口号  
                        String character = "utf8";// 默认字符集
                        String importDatabaseName = "prototype";//导入的目标数据库的名称  
                        String importPath = file.getAbsolutePath();//导入的目标文件所在的位置  
                        //第一步,获取登录命令语句  
                        StringBuilder sb = new StringBuilder();
                        sb.append("mysql -u").append(username);
                        sb.append(" -p").append(password);
                        sb.append(" -h").append(host);
                        sb.append(" -P").append(port);
                        sb.append(" --default-character-set=").append(character);
    
                        String loginCommand = sb.toString();
                        //第二步,获取切换数据库到目标数据库的命令语句  
                        String switchCommand = new StringBuilder("use ").append(importDatabaseName).toString();
                        //第三步,获取导入的命令语句  
                        String importCommand = new StringBuilder("source ").append(importPath).toString();
                        //需要返回的命令语句数组  
                        String[] commands = new String[] { loginCommand, switchCommand, importCommand };
    
                        Runtime runtime = Runtime.getRuntime();
                        Process process = runtime.exec(commands[0]);
                        //执行了第一条命令以后已经登录到mysql了,所以之后就是利用mysql的命令窗口  
                        //进程执行后面的代码  
                        OutputStream os = process.getOutputStream();
                        OutputStreamWriter writer = new OutputStreamWriter(os);
                        //命令1和命令2要放在一起执行  
                        writer.write(commands[1] + "\r\n" + commands[2]);
                        writer.flush();
                        writer.close();
                        os.close();
                        process.destroy();



4、通过Runtime.exec方法,直接调用mysql导入功能导入。1部操作
String cmd = "cmd /c mysql -uroot -proot --default-character-set=utf8  prototype < \""
                                    + file.getAbsolutePath() + "\"";
                    Runtime rt = Runtime.getRuntime();
                    Process p = rt.exec(cmd);

                    p.waitFor();

                    p.destroy();

猜你喜欢

转载自elvis4139.iteye.com/blog/2076101