2014-02-14 75 views
0

我將通過使用java代碼進行數據庫備份。此代碼正在執行正常,但我得到了int processComplete = runtimeProcess.waitFor();這種方法調用是返回整數爲1.所以最後我得到的消息,因爲無法創建備份爲sop。使用java進行Mysql數據庫備份

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

    } 
+0

嘗試讀取進程的錯誤流以查找可能的失敗 – Sanjeev

+0

System.out.println(executeCmd);''的輸出是什麼?你是否嘗試在命令行上執行這個確切的命令? –

+0

這是我得到的/bin/mysqldump -u root -p --add-drop-database -B rac -r D:/databasebackup/databasbac.sql – user3214269

回答

0

試試這個:

int processComplete = runtimeProcess.exitValue(); 

runtime info

希望這有助於

1

試試我的代碼爲備份數據庫中使用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; 
     } 
} 
2

我在通過JDBC從MYSQL進行數據庫備份時遇到類似的問題。所以我跑了下面的代碼。

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行。 這將創建您的數據庫備份。

如果你正在運行的

/bin/sh -c 

感謝這對LINUX代替

cmd.exe /c