Importing mysql database from java code

爱⌒轻易说出口 提交于 2019-12-11 23:54:30

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!