Exporting database through my java code

蓝咒 提交于 2019-12-07 09:30:15

问题


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.


回答1:


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.




回答2:


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

}



回答3:


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


来源:https://stackoverflow.com/questions/12217532/exporting-database-through-my-java-code

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