0

This is the code I created for exporting the database. The problem is, the file is not exported, and the code shows no error message.

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

How can I export the database to a path specified in the variable FilePath in the name FileName? How can I solve the issue?

And BTW, can i use the following to import the same??

String[] executeCmd = new String[]{"mysql", databaseName, "-u" + userName, "-p" + password, "-e"  + FileName };
4
  • What is your file path? is it relative or an absolute? My wild guess is it is exporting to a wrong directory. Commented Sep 7, 2012 at 7:08
  • I think you should make if(processComplete != 0){error} else {success} instead of if(processComplete==1), bcoz error-code returned may be other than 0 and 1. Commented Sep 7, 2012 at 7:09
  • @hims056: "the code shows no error message.". Gapchoos: Could you show the method invocation? I'm feeling it comes from the parameters. Have you tried to run the command (with the right parameters) in a shell? Commented Sep 7, 2012 at 7:11
  • The following code works with the parameters. and the export is successful. Process runtimeProcess = Runtime.getRuntime().exec("C:\\Program Files\\MySQL\\MySQL Server 5.1\\bin\\mysqldump -u root -proot mysql -r D:/backup.sql"); But i need to save the export file in a specific path. Commented Sep 7, 2012 at 8:30

3 Answers 3

1

You first try your executeCmd can run successfullly in db. In your executeCmd is syntax error. Your code left file name in execution command in

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

Check the manual

This works for me.

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

}
Sign up to request clarification or add additional context in comments.

Comments

1

Didn't you forget the Filename?

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

Comments

1

Make it as :

// 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.

I think you should make :

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

instead of

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

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

because error-code returned may be other than 0 and 1.

Suggestion : Use Apache Commons exec API, this is more sophisticated than Runtime.exec.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.