简体   繁体   中英

Restoring database in mysql using java command

its my code for restoring mysql databse using java command.

when i debug the code it get stuck at runtimeProcess.waitFor();

can any one provide solution for this problem?

public static boolean restoreDB(String dbUserName, String dbPassword,String dbName, String path) {

    String command ="mysql -u"+dbUserName+" -p"+dbPassword+" "+dbName+" < \""+path+"\"";

    Process runtimeProcess;
    try {

        runtimeProcess = Runtime.getRuntime().exec(command);
        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;
}

The process is likely either blocking waiting for input or it has written so much output that it has filled up the processes output buffer and is waiting for you to read the output. Either way you can get more information if you make sure to print out the stdout and stderr of the process:

private static class StreamReader implements Runnable {

    private InputStream stream;
    private volatile boolean finished = false;

    public StreamReader(InputStream stream) {
        this.stream = stream;
    }

    public void run() {
        Scanner scanner = new Scanner(stream);

        while(!finished) {
            System.out.println(scanner.nextLine());          
        }

    }

}

//...
runtimeProcess = Runtime.getRuntime().exec(command);
StreamReader stdoutReader = new StreamReader(runtimeProcess.getInputStream());
StreamReader stderrReader = new StreamReader(runtimeProcess.getErrorStream());
new Thread(stdoutReader).start();
new Thread(stderrReader).start();
int processComplete = -1;
try {
    processComplete = runtimeProcess.waitFor();
} finally {
    stdoutReader.finished = true;
    stderrReader.finished = true;
}

At last i found my answer .. thanks guys for your support, i have seen many of them having same problem,so it's my solution for you

we can't execute mysql restore command using Runtime.getRuntime().exec() function.

so i created a batch file and add this mysql restore command inside it and executed it.it's work fine. :)

public static void restoreDB(String dbUserName, String dbPassword,String dbName, String path) {
    try{
        //for linux .bat must replaced with .sh
        String destpath = "D:\\backup\\db.bat";

    //creating batch file 
    PrintWriter writer = new PrintWriter(destpath);
    String command = "mysql -u"+dbUserName+" -p"+dbPassword+" "+dbName+" < \""+path+"\"";
    writer.println(command);
    writer.close();

    //executing batch file
    Process run = Runtime.getRuntime().exec(destpath);
    System.out.println("file created");
    int processComplete = run.waitFor();

        if (processComplete == 0) {
            System.out.println("Backup created successfully");
            //return true;
        } else {
            System.out.println("Could not create the backup");
        }
    }catch(Exception ex)
    {
        ex.printStackTrace();
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM