Java implementation MySQL database backup (i)

Source: Internet
Author: User

After work, use your spare time to summarize how to use the Java language for MySQL database backup:

[Java]View PlainCopy
  1. Import Java.io.BufferedReader;
  2. Import Java.io.File;
  3. Import Java.io.FileOutputStream;
  4. Import java.io.IOException;
  5. Import Java.io.InputStreamReader;
  6. Import Java.io.OutputStreamWriter;
  7. Import Java.io.PrintWriter;
  8. /**
  9. * MySQL Database backup
  10. *
  11. * @author Gaohuanjie
  12. */
  13. Public class Mysqldatabasebackup {
  14. /** 
  15. * Java code Implementation MySQL database export
  16. *
  17. * @author Gaohuanjie
  18. * @param hostip MySQL database is located on the server address IP
  19. * @param userName User name required to enter the database
  20. * @param password The password required to enter the database
  21. * @param savepath Database Export File save path
  22. * @param filename Export file name of the filename database
  23. * @param databaseName The name of the database to be exported
  24. * @return returns True to indicate that the export was successful, otherwise false.
  25. */
  26. public Static boolean Exportdatabasetool (String HostIP, String userName, string password, string savepath, St Ring FileName, String databaseName) throws interruptedexception {
  27. File SaveFile = new File (Savepath);
  28. if (!savefile.exists ()) {//If the directory does not exist
  29. Savefile.mkdirs (); //Create a folder
  30. }
  31. if (!savepath.endswith (File.separator)) {
  32. Savepath = Savepath + file.separator;
  33. }
  34. PrintWriter printwriter = null;
  35. BufferedReader BufferedReader = null;
  36. try {
  37. PrintWriter = new PrintWriter (Newoutputstreamwriter (new FileOutputStream (Savepath + fileName), "UTF8"  ));
  38. Process process = Runtime.getruntime (). EXEC ("mysqldump-h" + HostIP + "-u" + userName + "-P" + Password + "  c3> "--set-charset=utf8" + databaseName);
  39. InputStreamReader InputStreamReader = new InputStreamReader (Process.getinputstream (), "UTF8");
  40. BufferedReader = new BufferedReader (InputStreamReader);
  41. String Line;
  42. While (line = Bufferedreader.readline ()) = null) {
  43. Printwriter.println (line);
  44. }
  45. Printwriter.flush ();
  46. if (process.waitfor () = = 0) {//0 indicates normal termination of the thread.
  47. return true;
  48. }
  49. }catch (IOException e) {
  50. E.printstacktrace ();
  51. } finally {
  52. try {
  53. if (bufferedreader! = null) {
  54. Bufferedreader.close ();
  55. }
  56. if (printwriter! = null) {
  57. Printwriter.close ();
  58. }
  59. } catch (IOException e) {
  60. E.printstacktrace ();
  61. }
  62. }
  63. return false;
  64. }
  65. public static void Main (string[] args) {
  66. try {
  67. if (Exportdatabasetool ("172.16.0.127", "root", " 123456", "D:/backupdatabase", "2014-10-14.") SQL ", " test ")) {
  68. SYSTEM.OUT.PRINTLN ("database successfully backed up!!!  ");
  69. } Else {
  70. SYSTEM.OUT.PRINTLN ("Database backup failed!!!  ");
  71. }
  72. } catch (Interruptedexception e) {
  73. E.printstacktrace ();
  74. }
  75. }
  76. }

Java implementation MySQL database backup (i)

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.