使用java进行Mysql数据库备份

时间:2014-02-14 09:00:40

标签: java mysql mysqldump

我将使用java代码进行数据库备份。这个代码很好但是我得到了 int processComplete = runtimeProcess.waitFor(); 这个方法调用将整数返回1。所以最后我得到的消息是无法创建备份为sop。

public static void main(String[] args) {

        String path = "D:/databasebackup/databasbac.sql";
        String username = "root";
        String password = "";
        String dbname = "rac";
        String executeCmd = "<Path to MySQL>/bin/mysqldump -u " + username + " -p" + password + " --add-drop-database -B " + dbname + " -r " + path;
        Process runtimeProcess;
        try {
//            System.out.println(executeCmd);//this out put works in mysql shell
            runtimeProcess = Runtime.getRuntime().exec(new String[] { "cmd.exe", "/c", executeCmd });
            System.out.println(executeCmd);
//            runtimeProcess = Runtime.getRuntime().exec(executeCmd);
            int processComplete = runtimeProcess.waitFor();
            System.out.println("processComplete"+processComplete);
            if (processComplete == 0) {
                System.out.println("Backup created successfully");

            } else {
                System.out.println("Could not create the backup");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

3 个答案:

答案 0 :(得分:2)

尝试使用java备份数据库的代码。它适用于我。

try {

     String filename = null;

     FileChooser.setVisible(true);

     int result = FileChooser.showSaveDialog(null);

     if (result == JFileChooser.APPROVE_OPTION) {
            filename = FileChooser.getSelectedFile().toString().concat(".sql");

            File file = new File(filename);

            if (file.exists()) {
                    Object option[] = {"Sim", "Nao"};

                    int opcao = JOptionPane.showOptionDialog(null, "aaa", "bbbb", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, option, option[0]);

                    if (opcao == JOptionPane.YES_OPTION) {
                             Runtime backup = Runtime.getRuntime();
                             backup.exec("C:\\wamp\\bin\\mysql\\mysql5.6.17\\bin\\mysqldump.exe -v -v -v --host=localhost --user=root --port=3306 --protocol=tcp --force --allow-keywords --compress --add-drop-table --result-file=" + filename + " --databases GIVE YOUR DATABSE NAME");
                             JOptionPane.showMessageDialog(null, "Backup succesfully");
                    } else {
                         FileChooserActionPerformed(evt);
                    }
           } else {
                 Runtime backup = Runtime.getRuntime();
                 backup.exec("C:\\wamp\\bin\\mysql\\mysql5.6.17\\bin\\mysqldump.exe -v -v -v --host=localhost --user=root --port=3306 --protocol=tcp --force --allow-keywords --compress --add-drop-table --result-file=" + filename + " --databases GIVE YOUR DATABASE NAME");
                 JOptionPane.showMessageDialog(null, "Backup succesfully");
           }
        }
    } catch (Exception e) {
          JOptionPane.showMessageDialog(null, e, "Error.!", 2);
}

这是我写过的DbOperation课程。

import com.mysql.jdbc.PreparedStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DbOperation {
       String url = "jdbc:mysql://localhost:3306/give your database name";
       String username = "your username";
       String password = "your password";
       Connection con = null;
       PreparedStatement pst = null;
       ResultSet rs = null;

       public Connection backupDB(){
              try{
                  con=DriverManager.getConnection(url, username, password);
              }catch(SQLException e){
                  System.out.println(e.getMessage());
              }
       return con;
      }
}

答案 1 :(得分:2)

我在MYSQL到JDBC的数据库备份中遇到了类似的问题。所以我运行了以下代码。

String path = C:\Program Files\MySQL\MySQL Server 5.7\bin\mysqldump

String command= "cmd.exe /c "
                    + "\"\""+path+"\"  "
                    + " --user="+UserInputs.getDbUserName()
                    + " --password="+UserInputs.getDbPassword()
                    + " --host="+UserInputs.getDbConnectionIP()
                    + " --protocol=tcp "
                    + " --port="+UserInputs.getDbConnectionPort()
                    + " --default-character-set=utf8 "
                    + " --single-transaction=TRUE "
                    + " --routines "
                    + " --events "
                    + "\""+UserInputs.getDbName()
                    +"\" "
                    + ">"
                    + " \""
                    + "D:\\MY DATA\\DB_Backup.sql"
                    + "\""
                    + " \"";

Runtime runtime = Runtime.getRuntime(command);

如果密码为空,请删除--password行。 这将创建数据库备份。

如果您在LINUX上运行此替换

cmd.exe /c

通过

/bin/sh -c

谢谢!

答案 2 :(得分:0)

试试这个:

int processComplete = runtimeProcess.exitValue();

runtime info

希望这有帮助