【问题标题】:restore mysql database using java in linux在linux中使用java恢复mysql数据库
【发布时间】:2017-03-05 07:47:42
【问题描述】:

我是 linux 用户,我想使用 java 恢复 mysql 数据库,我使用此代码来备份它,但我没有找到任何有效的方法来恢复它。此代码用于备份:

private static String ip = "localhost";
private static String port = "3306";
private static String database = "IMS";
private static String user = "root";
private static String pass = "pass";
private static String path = "/home/user/Desktop/project/";

    public static void backup() {
    Date dateNow = new Date();
    SimpleDateFormat dateformatyyyyMMdd = new SimpleDateFormat("yyyyMMdd");
    String date_to_string = dateformatyyyyMMdd.format(dateNow);
    System.out.println("date into yyyyMMdd format: " + date_to_string);
    String ss = "IMS.sql";
    String fullName = path + " " + date_to_string + " " + ss;
    String dumpCommand = "mysqldump " + database + " -h " + ip + " -u " + user + " -p" + pass;
    Runtime rt = Runtime.getRuntime();
    File test = new File(fullName);
    PrintStream ps;
    try {
        Process child = rt.exec(dumpCommand);
        ps = new PrintStream(test);
        InputStream in = child.getInputStream();
        int ch;
        while ((ch = in.read()) != -1) {
            ps.write(ch);
        //System.out.write(ch); //to view it by console
        }

        InputStream err = child.getErrorStream();
        while ((ch = err.read()) != -1) {
            System.out.write(ch);
        }
        } catch (Exception exc) {
        exc.printStackTrace();
       }
       }

【问题讨论】:

    标签: java mysql linux


    【解决方案1】:
    String user = "root";
        String pass = "1234";
        String[] restoreCmd = new String[]{"mysql", "--user=" + user, "--password=" + pass, "-e", "source " + path};
        Process process;
        try {
    
    
     process = Runtime.getRuntime().exec(restoreCmd);
            int procCom = process.waitFor();
            if (procCom == 0) {
                lbl_restore.setText("Restore Succes!");
            } else {
                lbl_restore.setText("Can't Restore!");
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
        }
    

    【讨论】:

      【解决方案2】:

      你可以用这个:

      public boolean restoreDatabase(String dbUserName, String dbPassword, String source) {
      
          String[] executeCmd = new String[]{"mysql", "--user=" + dbUserName, "--password=" + dbPassword, "-e", "source " + source};
      
          Process runtimeProcess;
      
          try {
              runtimeProcess = Runtime.getRuntime().exec(executeCmd);
              int processComplete = runtimeProcess.waitFor();
      
              if (processComplete == 0) {
                  log.info("Backup restored successfully with " + source);
                  return true;
              } else {
                  log.info("Could not restore the backup " + source);
              }
          } catch (Exception ex) {
              log.error(ex, ex.getCause());
          }
      
          return false;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-17
        • 2010-12-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-23
        相关资源
        最近更新 更多