Restore a mysql database in java

1

I have problems trying to restore a backup of a java database ... I have the code:

public boolean importar(String ruta){
    boolean bnd=false;
    String sentencia = rutaMySQL+"mysql -u "+user+" -p"+pass+" "+bd+" < "+ruta;
    Runtime rt = Runtime.getRuntime();
    try {
        System.out.println(sentencia);
        rt.exec(sentencia);
        bnd = true;
    } catch (IOException ex) {
        Logger.getLogger(MySQL.class.getName()).log(Level.SEVERE, null, ex);
    }
    return bnd;
}

This does not work for me, if it returns the value "true" but does not restore it. The strange thing is that the same command works perfectly for me from the CMD ... Does anyone know the error?

    
asked by José Licea de Ortiz 14.01.2017 в 06:20
source

1 answer

1

rt.exec (statement); remember that in doing so all the information remains in memory, that's why you do not see your backup reflected.

I had a problem similar to that, the explanation will pass you line by line so you understand how it works.

/*Este metodo recibira los siguientes parámetros.
 * @param host ruta de tu mysql
 * @param puert numero de puerto
 * @param  usuar
 * @param password
 * @param ruta aquí va la ruta donde deseas guardar tu backup
 * @param BD nombre de la base de datos
 */
public boolean importar(String host, String puert, String usuar, String password,String ruta,String BD){
 boolean ok=false;
    try{       
        //sentencia para crear el BackUp
         Process run = Runtime.getRuntime().exec(
        "cmd /c mysqldump --host=" + host + " --port=" + puert +
        " --user=" + usuar + " --password=" + password +
        " --complete-insert --extended-insert" +
        " " + ""+BD+"");
        //se guarda en memoria el backup
        InputStream in = run.getInputStream();
        //inicializamos para poder las lineas del backup
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        //para guardar los datos como string
        StringBuffer temp = new StringBuffer();
        int count;
        //hacemos una arreglo con una longitud apoximada de caracteres
        char[] cbuf = new char[10485760];
        //Empezamos a leer las líneas obtenidas
        while ((count = br.read(cbuf, 0, BUFFER)) != -1){
            //anexamos a nuestro buffer de string la línea leida
            //se agrega el arreglo obtenido y la longitud de este
            temp.append(cbuf, 0, count);
        }
        //cerramos los buffers
        br.close();
        in.close();        
        /* se crea y escribe el archivo SQL anexandole la ruta específica*/
        Archivo = new FileWriter(ruta);
        //para poder escribir sobre un fichero usaremos las clase PrintWriter
        PrintWriter pw = new PrintWriter(Archivo);
        //escribimos el fichero añadiendo salto de línea                                                    
        pw.println(temp.toString());  
        ok=true;
   }
    catch (Exception ex){
            ex.printStackTrace();
    } finally {
       try {           
         if (null != Archivo)
             Archivo.close();
       } catch (Exception e2) {
           e2.printStackTrace();
       }
    }   
    return ok; 
 }  
    
answered by 14.01.2017 / 19:11
source