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");
}
luchaninov
  • 6,792
  • 6
  • 60
  • 75

1 Answers1

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?

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thank you for response but still database is not restoring here is my code – user3610743 May 07 '14 at 07:03
  • 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 ?? – user3610743 May 07 '14 at 07:09
  • Does the `InputStreamConsumer` display any error messages? Does the command work from the command line? – MadProgrammer May 07 '14 at 07:10
  • no error and no change in database. process status is 1 – user3610743 May 07 '14 at 08:50
  • Where is the database located? – MadProgrammer May 07 '14 at 09:41
  • Is the program running from the correct directory context (`startIn`) – MadProgrammer May 07 '14 at 10:34
  • " C:\wamp\bin\mysql\mysql5.5.24\bin>mysql -u root -p sample < D:/data.sql " this code works from command line – user3610743 May 09 '14 at 03:32
  • There's in input redirection (`<`) in your example, and I' not sure if that would work any way... – MadProgrammer May 12 '14 at 03:06