4

I want to export my MySQL database using my java code. But I have not found any way to do. What I want to do that there is a button in my app as "Export Database". When that button is clicked, my database should be exported to specified path. I have used the following code but it does'nt worked :

 Runtime runtime = Runtime.getRuntime();
 runtime.exec("C:\\Program Files\\MySql\\MySql Server 5.5\\bin\\mysqldump -u root -p myDatabase> D:\\backup.sql");

How should I do this task. Thanks.

0

3 Answers 3

4

Two problems :

  • the space between -p and the password
  • the space inside the path to the executable

Prefer this :

 runtime.exec(new String[]{"C:\\Program Files\\MySql\\MySql Server 5.5\\bin\\mysqldump", "-u", "root", "-pmyDatabase" "> D:\\backup.sql"});

Note that if you have a problem with runtime.exec, you should look at the streams you can get from the returned Process. Not looking at those streams in case of error is a little like not looking at the exception when one is thrown.

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

1 Comment

How to achieve this from my Servlet? I will just remove the path before mysqldump and use this same piece of code?
4

Backup:

/******************************************************/
//Database Properties
/******************************************************/
String dbName = “dbName”;
String dbUser = “dbUser”;
String dbPass = “dbPass”;

/***********************************************************/
// Execute Shell Command
/***********************************************************/
String executeCmd = “”;
executeCmd = “mysqldump -u “+dbUser+” -p”+dbPass+” “+dbName+” -r backup.sql”;
}
Process runtimeProcess =Runtime.getRuntime().exec(executeCmd);
int processComplete = runtimeProcess.waitFor();
if(processComplete == 0){

out.println(“Backup taken successfully”);

} else {

out.println(“Could not take mysql backup”);

}

Restore:

/******************************************************/
//Database Properties
/******************************************************/
String dbName = “dbName”;
String dbUser = “dbUser”;
String dbPass = “dbPass”;

/***********************************************************/
// Execute Shell Command
/***********************************************************/
String executeCmd = “”;

executeCmd = new String[]{“/bin/sh”, “-c”, “mysql -u” + dbUser+ ” -p”+dbPass+” ” + dbName+ ” < backup.sql” };

}
Process runtimeProcess =Runtime.getRuntime().exec(executeCmd);
int processComplete = runtimeProcess.waitFor();
if(processComplete == 0){

out.println(“success”);

} else {

out.println(“restore failure”);

}

2 Comments

int processComplete = runtimeProcess.waitFor(); iam always getting processComplonly as 1 only...exception as "specified backup.sql cant find"
I need your full stack trace. you can paste it to pastebin.com and comment the link here or ask a new question in stackoverflow and give me the link.
1

You can try this.

   public void actionPerformed(ActionEvent evt)
    {
    private String m_MySqlPath=""; 
    ResultSet res=null;
    res = DBHandler.getInstance().executeQuery("select @@basedir",null);

          while(res.next())
          {
              m_MySqlPath=res.getString(1) ;
          }



          m_MySqlPath = m_MySqlPath.replace("\\Data\\", "\\bin\\");

    if (exportDB.isSelected()
    {    
    try {

          String executeCmd = m_MySqlPath + "\\mysqldump -u " + DB_USER
          +" -p" + DB_PASSWORD + " " + DB_NAME + " -r " + "\""+FilePath + "\\"
          + FileName+"\"";

        Process runtimeProcess = Runtime.getRuntime().exec(executeCmd, null);

        BufferedReader r=new BufferedReader(new InputStreamReader(runtimeProcess.getInputStream()));
        String s;
        while((s=r.readLine())!=null)
        {
            System.out.println(s);
        }

        return true;

    }
    catch (final Exception ex) {
        ex.printstackTrace();
        return false;
    }
}

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.