java 调用命令行 备份mysql数据库

  1. //方式1
  2. Stringcommand="D:\\mysql\\bin\\mysqldump-h127.0.0.1-uroot-p-ntd-R-dbname";
  3. Runtimeruntime=Runtime.getRuntime();
  4. Processprocess=runtime.exec(command);
  5. InputStreamReaderreader=newInputStreamReader(process.getInputStream());
  6. BufferedReaderbuffReader=newBufferedReader(reader);
  7. StringinStr;
  8. StringoutStr;
  9. StringBufferbuffer=newStringBuffer();
  10. while((inStr=buffReader.readLine())!=null){
  11. buffer.append(inStr+"\r\n");
  12. }
  13. outStr=buffer.toString();
  14. OutputStreamos=newFileOutputStream("D:\\sql.sql");
  15. OutputStreamWriterwriter=newOutputStreamWriter(os);
  16. writer.write(outStr);
  17. writer.flush();
  18. reader.close();
  19. buffReader.close();
  20. os.close();
  21. writer.close();
  22. //方式2
  23. Stringcommand="cmd/cmysqldump-h127.0.0.1-uroot-p-ntd-R-dbname";
  24. Runtimeruntime=Runtime.getRuntime();
  25. Processprocess=runtime.exec(command,null,newFile("D:\\mysql\\bin"));
  26. InputStreamReaderreader=newInputStreamReader(process.getInputStream());
  27. BufferedReaderbuffReader=newBufferedReader(reader);
  28. StringinStr;
  29. StringoutStr;
  30. StringBufferbuffer=newStringBuffer();
  31. while((inStr=buffReader.readLine())!=null){
  32. buffer.append(inStr+"\r\n");
  33. }
  34. outStr=buffer.toString();
  35. OutputStreamos=newFileOutputStream("D:\\sql.sql");
  36. OutputStreamWriterwriter=newOutputStreamWriter(os);
  37. writer.write(outStr);
  38. writer.flush();
  39. reader.close();
  40. buffReader.close();
  41. os.close();
  42. writer.close();

你可能感兴趣的:(mysql)