用Java导出MySQL不起作用

时间:2012-09-07 07:03:15

标签: java mysql export

这是我为导出数据库而创建的代码。问题是,文件未导出,代码不显示错误消息。

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 };

3 个答案:

答案 0 :(得分:1)

你没忘记文件名吗?

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

答案 1 :(得分: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更复杂。

答案 2 :(得分:1)

首先尝试执行executeCmd可以在db中成功运行。 在你的executeCmd中是语法错误。您的代码在

中的执行命令中保留了文件名
String executeCmd = "/" + dumbPath + "mysqldump -u " + dbUser
                    + " -p" + dbPass + " " + dbName + " -r " + FilePath + "\\"
                    + FileName;

Check the manual

这适合我。

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;
        }
    }

}