public class DumpFileClass{
public static boolean importDump(String host, String username, String password, String database, String dumpFilePath) {
String mysqlCommand = "mysql -h " + host + " -u " + username + " -p" + password +" " + database + " < " + dumpFilePath;
System.out.println(mysqlCommand);
try {
Process process = Runtime.getRuntime().exec(mysqlCommand);
logger.trace(process);
int exitCode = process.waitFor();
logger.trace(exitCode);
System.out.println(exitCode);
return exitCode == 0;
} catch (IOException | InterruptedException e) {
e.printStackTrace();
return false;
}
}
}
How can I push my .sql file (which contains db dump ) to another db on server using Java Code?
17 views Asked by Mayur At
1
You can avoid your dependence on the shell by writing to the process input stream. Also avoid dependence on passing a parameter of the database name to
mysql, as that's not relevant usually when restoring a dump: