美文网首页
ProcessBuilder 还原数据库

ProcessBuilder 还原数据库

作者: 长汀丶 | 来源:发表于2017-04-28 10:51 被阅读0次
package com;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Test {
    private final static String MYSQL = "C:/Program Files (x86)/MySQL/MySQL Server 5.0/bin/mysql";
    private final static String FILE_SQL = "D:/comment.sql";
    private final static String ROOT = "root";
    private final static String PASSWORD = "root";
    private final static String DATABASE = "test";
    private final static String HOST = "192.168.0.102";

    /**
     * @Name: main
     * @Description: TODO
     * @param args
     * @Author: wy
     * @Create Date: 2016-12-8 下午10:34:19
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ProcessBuilder pb = new ProcessBuilder(MYSQL ,"-h" + HOST,"-u" + ROOT,"-p" + PASSWORD,"-D" + DATABASE,"--execute=source " + FILE_SQL);
        pb.redirectErrorStream(true);
        try {
            Process process = pb.start();
            StringBuilder result = new StringBuilder();    
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));    

            String line;    
            while ((line = reader.readLine()) != null) {    
               result.append(line);    
            }    
            System.out.println(result.toString());
            process.waitFor();    
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


}

ps:ProcessBuilder 执行命令mysql命令的时候,不支持路径写法为 D:/aa.sql

相关文章

网友评论

      本文标题:ProcessBuilder 还原数据库

      本文链接:https://www.haomeiwen.com/subject/ftpozttx.html