Java 使用 Runtime 匯入 SQL File to Mysql

摘要:Java 使用 Runtime 匯入 SQL File to Mysql

 

第一招
    public void windowMySQLImport()
    {
        try
        {
            Runtime runtime = Runtime.getRuntime();
            String[] cmdarray = getImportCommand();
            Process process = runtime.exec(cmdarray[0]);
            //執行了第一條命令以後已經登錄到mysql了,所以之後就是利用mysql的命令窗口
            //進程執行後面的代碼
            OutputStream os = process.getOutputStream();
            OutputStreamWriter writer =  new  OutputStreamWriter(os);
            //命令1和命令2要放在一起執行
            writer.write(cmdarray[ 1 ] + "\r\n" + cmdarray[ 2 ]+ "\r\n" );
            writer.flush();
            writer.close();
            os.close();
            //等待執行完畢,若果在之後,馬上刪除檔案的話,會因找不到檔案,而失效。
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }
    
    public String[] getImportCommand()
    {
        String user_name = "root" ;
        String password = "password";
        String host ="localhost";
        String port = "3306";
        String db_name = "schema";
        String import_file = "D:\\insert.sql" ;
        String loginCommand = new StringBuffer().append("C:\\\\Program Files\\\\MySQL\\\\MySQL Server 5.5\\\\bin\\\\mysql -u").append(user_name)
                .append(" ").append("-p").append(password)
                .append(" ").append("-h").append(host)
                .append(" ").append("-P").append(port).toString();
        String switchCommand = new StringBuffer().append("use ").append(db_name).toString();
        String importCommand = new StringBuffer().append("source ").append(import_file).toString();
        String[] commands = new String[]{loginCommand,switchCommand,importCommand};
        return commands;
    }
第二招 (無效,不要模仿)
    public void windowMySQLImport()
    {
        try
        {
            Runtime runtime = Runtime.getRuntime();
            String[] cmdarray = getImportCommand();
            Process process = runtime.exec(cmdarray[0]);
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }
    
    public String[] getImportCommand()
    {
        String user_name = "root" ;
        String password = "password";
        String host ="localhost";
        String port = "3306";
        String db_name = "schema";
        String import_file = "D:\\insert.sql" ;
        String loginCommand = new StringBuffer().append("C:\\\\Program Files\\\\MySQL\\\\MySQL Server 5.5\\\\bin\\\\mysql -u").append(user_name)
                .append(" ").append("-p").append(password)
                .append(" ").append("-h").append(host)
                .append(" ").append("-P").append(port).append(" ").append(db_name).(" < ").append(import_file ).toString();;
        String[] commands = new String[]{loginCommand};
        return commands;
    }
 
忽略錯誤Statement之作法
加入--force (因為會有主鍵或unique的insert問題時,忽略錯誤)
    public String[] getImportCommand(String import_file)
    {
        String user_name = this.account;
        String password = this.password;
        String host = this.host;
        String port = this.port;
        String db_name = this.schema;
        String loginCommand = new StringBuffer().append(JamPusherUtils.mysql_exec_path + " -u").append(user_name)
                .append(" ").append("-p").append(password).append(" ").append("--force").toString();
        String switchCommand = new StringBuffer().append("use ").append(db_name).toString();
        String importCommand = new StringBuffer().append("source ").append(import_file).toString();
        String[] commands = new String[]{loginCommand,switchCommand,importCommand};
        return commands;
    }