从java

时间:2017-02-03 12:59:10

标签: java mysql

好的,我知道有很多与之相关的问题和文章,在关注它们并与它们一起玩之后我仍然无法成功。这就是我的代码

 import java.io.File;
 import java.io.IOException;
 import java.net.URISyntaxException;   
 import java.security.CodeSource;
 import javax.swing.JOptionPane;

public class BackupData 
{

public static void main(String[] args) {
    try 
    {

        /*NOTE: Getting path to the Jar file being executed*/
        /*NOTE: YourImplementingClass-> replace with the class executing the code*/
        CodeSource codeSource = BackupData.class.getProtectionDomain().getCodeSource();
        File jarFile = new File(codeSource.getLocation().toURI().getPath());
        String jarDir = jarFile.getParentFile().getPath();
        System.out.println("jarDir"+ jarDir);


        /*NOTE: Creating Database Constraints*/
        String dbName = "xyz";
        String dbUser = "root";
        String dbPass = "root";

        /*NOTE: Creating Path Constraints for folder saving*/
        /*NOTE: Here the backup folder is created for saving inside it*/
        String folderPath = jarDir + "\\backup";

        /*NOTE: Creating Folder if it does not exist*/
        File f1 = new File(folderPath);
        System.out.println("f1" + f1);
        f1.mkdir();

        /*NOTE: Creating Path Constraints for backup saving*/
        /*NOTE: Here the backup is saved in a folder called backup with the name backup.sql*/

         String savePath = "\"" + jarDir + "\\backup\\" + "1.sql\"";
         System.out.println("savepath" + savePath);

        /*NOTE: Used to create a cmd command*/
        String executeCmd = "C:\\Program Files\\MySQL\\MySQL Workbench 6.3 CE\\mysqldump -u " + dbUser + " -p " + dbPass + " --database " + dbName + " -r " + savePath;

        /*NOTE: Executing the command here*/
        Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);
        int processComplete = runtimeProcess.waitFor();

        /*NOTE: processComplete=0 if correctly executed, will contain other values if not*/


              if (processComplete == 0) 
          {
            System.out.println("Backup Complete");
          } 

             else 
          {
            System.out.println("Backup Failure");
            System.out.println(processComplete);
          }




    } 
    catch (URISyntaxException | IOException | InterruptedException ex) 
    {
        JOptionPane.showMessageDialog(null, "Error at Backuprestore" + ex.getMessage());
    }
      }
     }

此代码提供的输出 - 备份失败,2(过程完成值)

我无法理解我做错了什么?我错过了什么吗? 我无法弄清楚问题是什么,任何帮助都将不胜感激,谢谢。

1 个答案:

答案 0 :(得分:0)

为什么要这样做?为此,有一个名为mysqldump的命令行实用程序。

  

mysqldump客户端实用程序执行逻辑备份,生成一个集合   可以执行以重现原始的SQL语句   数据库对象定义和表数据。它转储一个或多个MySQL   用于备份或传输到另一个SQL服务器的数据库。 mysqldump   命令还可以生成CSV,其他分隔文本或XML的输出   格式。您还可以从mysql中找到以下链接   手册。

  1. https://dev.mysql.com/doc/refman/5.7/en/backup-methods.html

  2. https://dev.mysql.com/doc/refman/5.7/en/copying-databases.html

相关问题