Choose Your Language

Thursday 22 October 2015

java program to restore mysql database by executing sql file

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test;

/**
 *
 * @author Aravind Sankaran Nair
 */
public class RestoreDatabase {

    public static void main(String ar[]){
        //mysqlDatabaseBackUp();
        String databaseName="qualitychecker";
        String databaseUserName="root";
        String databasePassword="root";
                       /*                       "drive:\\folderName\\fileName"                          */
        String filePathToSqlFile="D:\\test\\qualitychecker_22-10-2015.sql";
        mysqlDatabaseRestore(databaseName,databaseUserName,databasePassword,filePathToSqlFile);
    }
 
    public static void mysqlDatabaseRestore(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");    
        } else{
            System.out.println("Could not restore the backup");
        }
     }
     catch(Exception ex)
     {
       
     }
}
}

1 comment: