我有同样的问题!
实际上,我唯一可以做的事情(在 Windows 上,还没有测试过其他平台)是使用批处理文件:
这是代码:
public class MysqlDatabase {
private int BUFFER = 10485760;
private String host, port, user, password, db;
public MysqlDatabase(String host, String port, String user, String password, String db) {
this.host = host;
this.port = port;
this.user = user;
this.password = password;
this.db = db;
}
public boolean restoreDatabase(String filepath) throws Exception {
String comando = "mysql " + db + " --host=" + host + " --port=" + port
+ " --user=" + user + " --password=" + password
+ " < " + filepath;
File f = new File("restore.bat");
FileOutputStream fos = new FileOutputStream(f);
fos.write(comando.getBytes());
fos.close();
Process run = Runtime.getRuntime().exec("cmd /C start restore.bat ");
return true;
}
public String getFull() throws Exception {
Process run = Runtime.getRuntime().exec(
"mysqldump --host=" + host + " --port=" + port
+ " --user=" + user + " --password=" + password
+ " --opt "
+ "" + db);
InputStream in = run.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
StringBuilder temp = new StringBuilder();
int count;
char[] cbuf = new char[BUFFER];
while ((count = br.read(cbuf, 0, BUFFER)) != -1) {
temp.append(cbuf, 0, count);
}
br.close();
in.close();
return temp.toString();
}}