How to restore a MySQL database backup using Java

前端 未结 8 2158
日久生厌
日久生厌 2020-12-10 22:57

I was able to create a backup of my current mysql database as .SQL file using the mysqldump.exe with the help of the following java code. <

8条回答
  •  盖世英雄少女心
    2020-12-10 23:43

    public static boolean restoreDB(String dbName, String dbUserName, String dbPassword, String source) {  
    String[] executeCmd = new String[]{"mysql", "--user=" + dbUserName, "--password=" + dbPassword, dbName,"-e", " source "+source};  
    Process runtimeProcess;  
    try {  
    runtimeProcess = Runtime.getRuntime().exec(executeCmd);  
    int processComplete = runtimeProcess.waitFor();  
    if (processComplete == 0) {  
        System.out.println("Backup restored successfully");  
        return true;  
    }  
    } else {  
         System.out.println("Could not restore the backup");  
           }  
            } catch (Exception ex) {  
                ex.printStackTrace();  
            }  
            return false;  
    }  
    

    source example : "E:\\My Backup\\Test\\file.sql"

提交回复
热议问题