Guest User

Untitled

a guest
Jun 23rd, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. public boolean importar(String ruta){
  2. boolean bnd=false;
  3. String sentencia = rutaMySQL+"mysql -u "+user+" -p"+pass+" "+bd+" < "+ruta;
  4. Runtime rt = Runtime.getRuntime();
  5. try {
  6. System.out.println(sentencia);
  7. rt.exec(sentencia);
  8. bnd = true;
  9. } catch (IOException ex) {
  10. Logger.getLogger(MySQL.class.getName()).log(Level.SEVERE, null, ex);
  11. }
  12. return bnd;
  13. }
  14.  
  15. /*Este metodo recibira los siguientes parámetros.
  16. * @param host ruta de tu mysql
  17. * @param puert numero de puerto
  18. * @param usuar
  19. * @param password
  20. * @param ruta aquí va la ruta donde deseas guardar tu backup
  21. * @param BD nombre de la base de datos
  22. */
  23. public boolean importar(String host, String puert, String usuar, String password,String ruta,String BD){
  24. boolean ok=false;
  25. try{
  26. //sentencia para crear el BackUp
  27. Process run = Runtime.getRuntime().exec(
  28. "cmd /c mysqldump --host=" + host + " --port=" + puert +
  29. " --user=" + usuar + " --password=" + password +
  30. " --complete-insert --extended-insert" +
  31. " " + ""+BD+"");
  32. //se guarda en memoria el backup
  33. InputStream in = run.getInputStream();
  34. //inicializamos para poder las lineas del backup
  35. BufferedReader br = new BufferedReader(new InputStreamReader(in));
  36. //para guardar los datos como string
  37. StringBuffer temp = new StringBuffer();
  38. int count;
  39. //hacemos una arreglo con una longitud apoximada de caracteres
  40. char[] cbuf = new char[10485760];
  41. //Empezamos a leer las líneas obtenidas
  42. while ((count = br.read(cbuf, 0, BUFFER)) != -1){
  43. //anexamos a nuestro buffer de string la línea leida
  44. //se agrega el arreglo obtenido y la longitud de este
  45. temp.append(cbuf, 0, count);
  46. }
  47. //cerramos los buffers
  48. br.close();
  49. in.close();
  50. /* se crea y escribe el archivo SQL anexandole la ruta específica*/
  51. Archivo = new FileWriter(ruta);
  52. //para poder escribir sobre un fichero usaremos las clase PrintWriter
  53. PrintWriter pw = new PrintWriter(Archivo);
  54. //escribimos el fichero añadiendo salto de línea
  55. pw.println(temp.toString());
  56. ok=true;
  57. }
  58. catch (Exception ex){
  59. ex.printStackTrace();
  60. } finally {
  61. try {
  62. if (null != Archivo)
  63. Archivo.close();
  64. } catch (Exception e2) {
  65. e2.printStackTrace();
  66. }
  67. }
  68. return ok;
  69. }
  70.  
  71. Runtime rt = Runtime.getRuntime();
  72. rt.exec(backus);
  73. JOptionPane.showMessageDialog(null, "Backus Importado: "+ruta);
  74. }catch(Exception ex){
  75. JOptionPane.showMessageDialog(null, ex.getMessage());
  76. }
  77. }
Add Comment
Please, Sign In to add comment