0

Here is my code for mysql database restore code .when i tried this code app works without exception but application get hangs and database is not restored ..please help me

String databaseName = "sample"; //database name
String userName = "root"; // MySQL username
String password = ""; // MySQL password
int processComplete; // this variable for verify the process

String[] executeCmd = new String[]{"C:\\wamp\\bin\\mysql\\mysql5.5.24\\bin\\mysql",
        databaseName, "-u" + userName, "-p" + password, "-e", " source D:/data.sql"};

System.out.println(executeCmd);
Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);// execute the command
processComplete = runtimeProcess.waitFor();
System.out.println(processComplete);

if (processComplete == 1) { // if return value equal to 1 then failed the process
    JOptionPane.showMessageDialog(null, "Restore Failed");
} else if (processComplete == 0) {{// if return value equal to 0 then failed the process
    JOptionPane.showMessageDialog(null, "Restore Completed");
}

1 Answer 1

0

I suspect that the last parameter is been mishandled

String[] executeCmd = new String[]{
    "C:\\wamp\\bin\\mysql\\mysql5.5.24\\bin\\mysql", 
    databaseName, 
    "-u" + userName, 
    "-p" + password, 
    "-e", 
    " source D:/data.sql" }

It should probably look more like...

String[] executeCmd = new String[]{
    "C:\\wamp\\bin\\mysql\\mysql5.5.24\\bin\\mysql", 
    databaseName, 
    "-u" + userName, 
    "-p" + password, 
    "-e", 
    "source", 
    "D:/data.sql" }

Each element in the array will be a separate argument passed to the command, this allows you the flexibility of passing arguments that have spaces in them without need to first escape them using quotes

You should consider using ProcessBuilder instead of trying to build the Process yourself, apart from allowing you to re-direct the error stream to the input stream, it also allows you to specify the starting context for the process.

You should also be reading the output of the process (via it's InputStream) which would possibly highlight issues and may also allow the process to exit (as some process won't exit until there stdout is read completely)

For example: How do I execute Windows commands in Java?

Sign up to request clarification or add additional context in comments.

8 Comments

Thank you for response but still database is not restoring here is my code
Thank you for response but still database is not restoring and process value is 1 here is my code <<--->> String[] executeCmd = new String[]{"C:\\wamp\\bin\\mysql\\mysql5.5.24\\bin\\mysql",databaseName,"-u" + userName,"-p" + password,"-e","source","D:/data.sql" }; ProcessBuilder pb = new ProcessBuilder(executeCmd); pb.redirectError(); Process p = pb.start(); InputStreamConsumer isc = new InputStreamConsumer(p.getInputStream()); isc.start(); int exitCode = p.waitFor(); isc.join(); System.out.println("Process terminated with " + exitCode); <<-------------->> any solution i am using win7 ??
Does the InputStreamConsumer display any error messages? Does the command work from the command line?
no error and no change in database. process status is 1
Where is the database located?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.