Exporting database through my java code

不打扰是莪最后的温柔 提交于 2019-12-05 14:55:41

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.

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”);

}

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;
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!