问题
This code do restore mysql tables but application almost goes into sleep(20min plus). and my database Routines do not come in mysql workbench, can somebody tell me what is the actual solution?
public static boolean restoreDB(String dbName, String dbUserName, String dbPassword, String source) {
String[] executeCmd = new String[]{"C:\\Program Files\\MySQL\\MySQL Server 5.5\\bin\\mysql", "--user=" + dbUserName, "--password=" + dbPassword, "-e", "source \"D:/khokher/mydb.sql\""};
Process runtimeProcess;
try {
runtimeProcess = Runtime.getRuntime().exec(executeCmd);
int 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();
}
return false;
}
回答1:
When using the Runtime.exec
method, you have to read the output from the process it creates. Otherwise the process fills its output buffer and stops to wait until you empty the buffer by reading the output.
To read the output from a process you get the input streams from the Process
object that exec
returned. You can read the standard output from the stream returned by getInputStream
and the standard error from the stream returned by getErrorStream
.
If you can, use the Apache Commons Exec library. It makes launching an external processes and handling its input and output easier.
来源:https://stackoverflow.com/questions/13227057/importing-mysql-database-from-java-code