java—尝试从lan网络上的不同pc获取db备份会生成空sql文件

wlp8pajw  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(391)

当我尝试从另一台pc(称为pac 1)获取数据库备份时,该pc通过访问计算机(称为pac 2)服务器连接到使用mysqldump的本地计算机的同一个本地网络
数据库在pc1上,我想通过执行java程序在pc机上生成一个数据库备份。目前我实现了这一点,但有一个错误,所以创建了一个空的sql文件。 userPathForDbBackup 是一个字符串,它采用从user到filechooser的路径 mySqlPath 是一个变量,它是pc2上的文件路径

String mySqlPath="C:\\Program Files\\MySQL\\MySQL Server 5.6\\bin\\mysqldump.exe";
String name="name";
String pathFinal=userPathForDbBackup + "\\Backup_" + name + ".sql";
String[] command = {mySqlPath,"mysqldump", "-h"+dbh, "-u "+dbUser, "-p" + dbPass, dbName,">"+pathFinal};
try{
  connection = create_connection.jdbc.getCon();
  String executeCmd = "";
  if(connection!=null){
    executeCmd = "mysqldump -h"+dbh+"-u"+dbUser+" -p"+dbPass+" "+dbName+"file.sql" ;
    System.out.println();
    ProcessBuilder pb = new ProcessBuilder(command);

    Process runtimeProcess =pb.start();
    // Process runtimeProcess =Runtime.getRuntime().exec(command);
    int processComplete = runtimeProcess.waitFor();
    if(processComplete == 0){
      System.out.println("Backup taken successfully");
    } else {
      System.out.println("Could not take mysql backup");
    }
  } else{
    System.out.println("connection not sucess");
  }
}catch (Exception e) {
  e.printStackTrace();
}

这不会引发任何异常,但会在用户选择的目录中创建一个空sql文件。

6uxekuva

6uxekuva1#

String dbName = "";
            String dbUser = "";
            String dbPass = "";
            String dbh = "";
            String dbport = "";
            Connection connection;

//userPathForDbBackup is user selected path from file chooser

            String pathFinal=userPathForDbBackup + "\\Backup_" + name + ".sql";
            System.out.println(userPathForDbBackup+"before");
            userPathForDbBackup= userPathForDbBackup+"\\Backup_name.sql";
            System.out.println(userPathForDbBackup+"after");

            String folderPath = userPathForDbBackup.substring(0, userPathForDbBackup.lastIndexOf("\\")) ;

            File path =new File(userPathForDbBackup);

            String[] executeCmd = new String[]{"C:/Program Files/MySQL/MySQL Server 5.6/bin/mysqldump.exe", "--user=" + dbUser, "--password=" + dbPass,""+ dbName,"-r"+path};

            try{

                connection = create_connection.jdbc.getCon();

                if(connection!=null){
                    Process runtimeProcess =Runtime.getRuntime().exec(executeCmd);
                    int processComplete = runtimeProcess.waitFor();
                    if (processComplete != 0) { //4
                        // something went wrong
                        InputStream errorStream = runtimeProcess.getErrorStream();
                        byte[] buffer = new byte[errorStream.available()];
                        errorStream.read(buffer);

                        System.out.println(new String(buffer));
                    }
                } else{
                    System.out.println("connection not sucess");
                }
            }catch (Exception e) {
                e.printStackTrace();
            }

终于有了这个密码

相关问题