backup mysql database java code

前端 未结 3 889
终归单人心
终归单人心 2020-12-10 19:12

I tried to run the following code which is to create a backup of my database but it shows some run time errors.

But, I tried to run the Syst

相关标签:
3条回答
  • 2020-12-10 19:53

    Please check whether your Global PATH environment variable has <Path to MySQL>\bin in it (Do a echo %PATH% and see). Effectively you should be able to type your System.out.println() content in a Plain DOS prompt and should be able to run it.

    Even with that if it doesn't work try changing the code to execute like below

    runtimeProcess = Runtime.getRuntime().exec(new String[] { "cmd.exe", "/c", executeCmd });
    

    This should ideally fix the issue.

    UPDATE:

    If you don't have it in the PATH environment variable change the code to following

    String executeCmd = "<Path to MySQL>/bin/mysqldump -u " + dbUserName + " -p" + dbPassword + " --add-drop-database -B " + dbName + " -r " + path;
    
    0 讨论(0)
  • 2020-12-10 19:58

    runtimeProcess = Runtime.getRuntime().exec(new String[] { "cmd.exe", "/c", executeCmd });

    i tried this one but it didn't work so i replaced it with

    this runtimeProcess = Runtime.getRuntime().exec(executeCmd);

    and it worked

    0 讨论(0)
  • 2020-12-10 19:59

    I solved this problem by giving full path to mysqldump.exe

    You can get SO environment variables by

    Map<String, String> env = System.getenv();
    final String LOCATION = env.get("MYSQLDUMP");
    

    I setup the system variable like this :

    • Variable Name : MYSQLDUMP
    • Value : D:\xampp\mysql\bin\mysqldump.exe

    Now just execute this code below,

    String executeCmd = LOCATION+" -u " + DBUSER + " --add-drop-database -B " + DBNAME + " -r " + PATH + ""+FILENAME
    Process runtimeProcess = = Runtime.getRuntime().exec(executeCmd);
    

    Note : If you don't have configured password for mysql server just remove the -p PASSWORD attribute from the code. And don't forget to restart your computer after creating a new system variable.

    0 讨论(0)
提交回复
热议问题