0

这是我为导出数据库创建的代码。问题是,文件没有导出,代码没有显示错误信息。

public boolean exportDatabase(String fromServer,
                              String FileName,
                              String FilePath,
                              int ExportOpions) {
  try {     
    String dbName ="NMSAzzist";
    String dbUser = "root";
    String dbPass ="root";  
    String dumbPath = "C:\\Program Files\\MySQL\\MySQL Server 5.1\\bin\\";
    String executeCmd = dumbPath+"mysqldump -u "+dbUser+ "-p"+dbPass+" "+dbName+ "-r "+FilePath+ "";
    Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);
    int processComplete = runtimeProcess.waitFor();
    if (processComplete == 1) { // if values equal 1 process failed
      JOptionPane.showMessageDialog(null, "Backup Failed");//display message
    } else if (processComplete == 0) {
      JOptionPane.showMessageDialog(null, "\n Backup created Successfully..");
      // display message
    }
    return true;
  } catch (final Exception ex) {
    NmsLogger.writeErrorLog("Database Connection Failed ", ex.toString());
    NmsLogger.writeDebugLog(ex);
    return false;
  }

如何将数据库导出到FilePath名称中变量中指定的路径FileName?我该如何解决这个问题?

顺便说一句,我可以使用以下内容导入相同的内容吗?

String[] executeCmd = new String[]{"mysql", databaseName, "-u" + userName, "-p" + password, "-e"  + FileName };
4

3 回答 3

1

你没有忘记文件名吗?

String executeCmd =dumbPath+"mysqldump -u "+dbUser+ "-p"+dbPass+" "+dbName+ "-r "+FilePath+"\\"+Filename"";
于 2012-09-07T07:09:30.850 回答
1

使其为:

// you did not give file name.
String executeCmd = "cmd " + dumbPath+"mysqldump -u "+dbUser+ "-p"+dbPass+" "+dbName+ "-r "+FilePath+ "\\" + filename;

// I tried running I am getting error code 13.

我认为你应该做:

if(processComplete != 0) {
   //error with error code
} else {
   //success
} 

代替

if (processComplete == 1) {// if values equal 1 process failed
      System.out.println("Backup failed");
}

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

因为返回的错误代码可能不是 0 和 1。

建议:使用Apache Commons exec API,这个比Runtime.exec.

于 2012-09-07T07:17:36.117 回答
1

您首先尝试您的 executeCmd 可以在 db 中成功运行。在您的 executeCmd 中是语法错误。您的代码在执行命令中留下文件名

String executeCmd = "/" + dumbPath + "mysqldump -u " + dbUser
                    + " -p" + dbPass + " " + dbName + " -r " + FilePath + "\\"
                    + FileName;

检查手册

这对我有用。

public class exportDataBase {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        exportDatabase("", "Sma_test.sql", "C:", 0);

    }

    public static boolean exportDatabase(String fromServer, String FileName,
            String FilePath, int ExportOpions) {
        try {

            String dbName = "dmsdev";
            String dbUser = "root";
            String dbPass = "root";

            String dumbPath = "C:\\Program Files\\MySQL\\MySQL Server 5.1\\bin\\";

            String executeCmd = "/" + dumbPath + "mysqldump -u " + dbUser
                    + " -p" + dbPass + " " + dbName + " -r " + FilePath + "\\"
                    + FileName;

            System.out.println(executeCmd);
            Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);
            int processComplete = runtimeProcess.waitFor();

            System.out.println("processComplete: " + processComplete);

            if (processComplete == 1) {// if values equal 1 process failed
                System.out.println("Backup failed");
            }

            else if (processComplete == 0) {
                System.out.println("Backup Success");

            }
            return true;
        } catch (final Exception ex) {
            System.out.println("Connection failed");
            return false;
        }
    }

}
于 2012-09-07T07:49:43.570 回答