这就是我所拥有的:

List<String> restoreCmds = new ArrayList<>();
restoreCmds.add("mysql");
restoreCmds.add("-h" + host);
restoreCmds.add("-u" + login);
restoreCmds.add("-p" + pass);
restoreCmds.add("-e 'source dump.sql'");
restoreCmds.add(targetDB);

ProcessBuilder pb = new ProcessBuilder(restoreCmds);
pb.redirectOutput(Redirect.INHERIT);
pb.redirectError(Redirect.INHERIT);
Process pr = pb.start();

int exitVal = pr.waitFor()

我得到以下错误:
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''source dump.sql'' at line 1

最佳答案

您不需要为源添加报价。
我把这条线改成了following,它对我很有用。

restoreCmds.add("-e source dump.sql");

10-08 19:56