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
网友评论