Back up MySQL databases by calling Java commands

Source: Internet
Author: User
Back up MySQL databases by calling Java commands

Java code
  1. String command = "CMD/C:/program files/MySQL Server 5.0/bin> mysqldump-H localhost-u root-P aijia> E:/aijia. dmp ";
  2. Try {
  3. Process = runtime.getruntime(cmd.exe C (command );
  4. Inputstreamreader IR = new inputstreamreader (Process
  5. . Getinputstream ());
  6. Linenumberreader input = new linenumberreader (IR );
  7. String line;
  8. While (line = input. Readline ())! = NULL)
  9. System. Out. println (line );
  10. Input. Close ();
  11. } Catch (ioexception e ){
  12. E. printstacktrace ();
  13. }
  14. In addition
  15. First, set the MySQL environment variable (add % mysql_home %/bin in path) and restart the computer.
  16. Complete code:
  17. /**
  18. * @ Param ARGs
  19. */
  20. Public static void main (string [] ARGs ){
  21. /*
  22. * Backup and import are reciprocal processes.
  23. * Backup: The program calls the MySQL BACKUP command to read the console input stream information and write the. SQL file;
  24. * Import: The program calls the MySQL import command to write the Information read from the. SQL file to the output stream on the console.
  25. * Note: ">" and "<" are unavailable at this time.
  26. */
  27. Backup ();
  28. Load ();
  29. }
  30. /**
  31. * Check whether an SQL file can be used as an import file through backup: use Notepad and Ultra to separate the SQL file.
  32. * Edit is enabled. If no garbled characters are displayed in Chinese, the source file can be used for import (regardless of the encoding format of the SQL file or the DB encoding format)
  33. */
  34. Public static void backup (){
  35. Try {
  36. Runtime RT = runtime. getruntime ();
  37. // Call the CMD of MySQL:
  38. Process child = RT
  39. . Exec ("mysqldump-u root -- Set-charset = utf8 bjse act_obj"); // set the export encoding to utf8. It must be utf8.
  40. // Write the console output information in the process execution to the. SQL file, which generates a backup file. Note: If you do not read the console information, the process may be blocked and cannot run.
  41. Inputstream in = Child. getinputstream (); // the output information of the console is used as the input stream.
  42. Inputstreamreader xx = new inputstreamreader (in, "utf8"); // sets the output stream encoding to utf8. This must be utf8; otherwise, garbled characters are read from the stream.
  43. String instr;
  44. Stringbuffer sb = new stringbuffer ("");
  45. String outstr;
  46. // Combine the console output string
  47. Bufferedreader BR = new bufferedreader (XX );
  48. While (instr = Br. Readline ())! = NULL ){
  49. SB. append (instr + "/R/N ");
  50. }
  51. Outstr = sb. tostring ();
  52. // The SQL target file to be used for import:
  53. Fileoutputstream fout = new fileoutputstream (
  54. "E:/mysql-5.0.27-win32/bin/bjse22. SQL ");
  55. Outputstreamwriter writer = new outputstreamwriter (fout, "utf8 ");
  56. Writer. Write (outstr );
  57. // Note: if the file is written as a buffer, Chinese characters may be garbled. You can avoid using the flush () method.
  58. Writer. Flush ();
  59. // Do not forget to close the input/output stream
  60. In. Close ();
  61. XX. Close ();
  62. BR. Close ();
  63. Writer. Close ();
  64. Fout. Close ();
  65. System. Out. println ("/* output OK! */");
  66. } Catch (exception e ){
  67. E. printstacktrace ();
  68. }
  69. }
  70. /**
  71. * Import
  72. *
  73. */
  74. Public static void load (){
  75. Try {
  76. String fpath = "E:/mysql-5.0.27-win32/bin/bjse22. SQL ";
  77. Runtime RT = runtime. getruntime ();
  78. // Call the CMD of MySQL:
  79. Process child = rt.exe C ("mysql-u root bjse ");
  80. Outputstream out = Child. getoutputstream (); // the input information of the console is used as the output stream.
  81. String instr;
  82. Stringbuffer sb = new stringbuffer ("");
  83. String outstr;
  84. Bufferedreader BR = new bufferedreader (New inputstreamreader (
  85. New fileinputstream (fpath), "utf8 "));
  86. While (instr = Br. Readline ())! = NULL ){
  87. SB. append (instr + "/R/N ");
  88. }
  89. Outstr = sb. tostring ();
  90. Outputstreamwriter writer = new outputstreamwriter (Out, "utf8 ");
  91. Writer. Write (outstr );
  92. // Note: if the file is written as a buffer, Chinese characters may be garbled. You can avoid using the flush () method.
  93. Writer. Flush ();
  94. // Do not forget to close the input/output stream
  95. Out. Close ();
  96. BR. Close ();
  97. Writer. Close ();
  98. System. Out. println ("/* load OK! */");
  99. } Catch (exception e ){
  100. E. printstacktrace ();
  101. }
  102. }

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.