Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- }
- /*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;
- }
- Runtime rt = Runtime.getRuntime();
- rt.exec(backus);
- JOptionPane.showMessageDialog(null, "Backus Importado: "+ruta);
- }catch(Exception ex){
- JOptionPane.showMessageDialog(null, ex.getMessage());
- }
- }
Add Comment
Please, Sign In to add comment