我使用此代码导入(恢复).sql文件:
String command2="/Users/macbookpro/mysql-5.6.16-osx10.7-x86_64/bin/mysql --host mydb.ckwwsnbi2lvi.us-west-2.rds.amazonaws.com --port 3306 -u XXX -pXXXX testcomm </Users/macbookpro/Downloads/world3.sql"
Process runtimeProcess;
try {
runtimeProcess = Runtime.getRuntime().exec(command2);
processComplete = runtimeProcess.waitFor();
if (processComplete == 0) {
System.out.println("Backup restored successfully");
// return true;
} else {
System.out.println("Could not restore the backup");
}
} catch (Exception ex) {
ex.printStackTrace();
}
该命令在终端中运行良好,但在运行Java代码时它无效。 任何帮助将不胜感激。
答案 0 :(得分:0)
感谢您的帮助。问题已经解决了。 我用String数组替换字符串并使用&#39; -e source&#39;而不是&#39;&lt;&#39;。
这是新命令:
String[] command2 = new String[]{"/Users/macbookpro/mysql-5.6.16-osx10.7-x86_64/bin/mysql","--host=" + ad,"--port=" + pt,"--user=" + u,"--password="+ p,"testcomm","-e"," source "+"/Users/macbookpro/Downloads/world3.sql"};
这link对我有帮助。