使用Java创建数据库备份

时间:2015-11-10 17:36:48

标签: java mysql

我正在使用此代码创建备份,但每次运行时,我都会得到结果

Couldn't Create the backup!

我已经阅读了很多教程。他们中的大多数使用相同的代码,但我不知道是什么问题。我正在使用MySQL和PATH环境变量设置。

public class Temp {

public void restoreDB(String path){

    String executeCmd = "mysql -u root -proot -B temp1 -r " + path;

    System.out.println(executeCmd);

    Process runtimeProcess;

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

       int processComplete = runtimeProcess.waitFor();

       System.out.println(processComplete);

       if(processComplete == 0)
       {
          System.out.println("Backup Created Successfully !");
       }
       else
       {
          System.out.println("Couldn't Create the backup !");
       }
    }
    catch(Exception ex)
    {
       ex.printStackTrace();
    }

   }

      public static void main(String[]args){

         new Temp().restoreDB("C:/File.sql");
      }


}

0 个答案:

没有答案