Java implementation MySQL database backup (ii)

Source: Internet
Author: User

Rights statement: This article for Bo Master original article, without Bo Master permission not reproduced.

Blog "Java implementation MySQL database backup (i)" using I/O stream to implement the MySQL database backup, this method is more complicated, the following describes another way to back up the MySQL database:

[Java]View PlainCopy
  1. Import Java.io.File;
  2. Import java.io.IOException;
  3. /**
  4. * MySQL Database backup
  5. *
  6. * @author Gaohuanjie
  7. */
  8. Public class Mysqldatabasebackup {
  9. /** 
  10. * Java code Implementation MySQL database export
  11. *
  12. * @author Gaohuanjie
  13. * @param hostip MySQL database is located on the server address IP
  14. * @param userName User name required to enter the database
  15. * @param password The password required to enter the database
  16. * @param savepath Database Export File save path
  17. * @param filename Export file name of the filename database
  18. * @param databaseName The name of the database to be exported
  19. * @return returns True to indicate that the export was successful, otherwise false.
  20. */
  21. public Static boolean Exportdatabasetool (String HostIP, String userName, string password, string savepath, St Ring FileName, String databaseName) {
  22. File SaveFile = new File (Savepath);
  23. if (!savefile.exists ()) {//If the directory does not exist
  24. Savefile.mkdirs (); //Create a folder
  25. }
  26. if (!savepath.endswith (File.separator)) {
  27. Savepath = Savepath + file.separator;
  28. }
  29. StringBuilder StringBuilder = new StringBuilder ();
  30. Stringbuilder.append ("mysqldump"). Append ("--opt"). Append ("-H").append (HostIP);
  31. Stringbuilder.append ("--user="). Append (UserName). Append ("--password="). Append (password). Append ("--  Lock-all-tables=true ");
  32. Stringbuilder.append ("--result-file="). Append (Savepath + fileName). Append ("--default-character-set=utf8"  ). append (DatabaseName);
  33. try {
  34. Process process = Runtime.getruntime (). EXEC (stringbuilder.tostring ());
  35. if (process.waitfor () = = 0) {//0 indicates normal termination of the thread.
  36. return true;
  37. }
  38. } catch (IOException e) {
  39. E.printstacktrace ();
  40. } catch (Interruptedexception e) {
  41. E.printstacktrace ();
  42. }
  43. return false;
  44. }
  45. public static void Main (string[] args) throws interruptedexception {
  46. if (Exportdatabasetool ("172.16.0.127", "root", " 123456", "D:/backupdatabase", "2014-10-14.") SQL ", " test ")) {
  47. SYSTEM.OUT.PRINTLN ("database backup succeeded!!!  ");
  48. } Else {
  49. SYSTEM.OUT.PRINTLN ("Database backup failed!!!  ");
  50. }
  51. }
  52. }

Java implementation MySQL database backup (ii)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.