Use Java to import and export MySql Databases

Source: Internet
Author: User

The general idea of using Java for Mysql Database Import and Export is to use Java to call the command window to execute the corresponding commands.

The command for MySql to export a database is as follows:

 
 
  1. mysqldump -uusername -ppassword -hhost -Pport exportDatabaseName > exportPath 

Using Java to call the command window to execute commands to import MySql into the database is generally divided into three steps:

Step 1: log on to the Mysql database. You can also specify the database to log on to when logging on to the database. If you specify the database, skip step 2;

Step 2: Switch the database to the target database to be imported

Step 3: Use commands to start importing

When exporting, pay attention to the running environment of the command statement. If you have added the bin in the mysql installation path

In the path variable of the system, you can directly use the command statement during export. Otherwise, you need to execute the command statement

Add the path where the command is located, that is, the mysqldump command under the bin in the mysql installation path.

The basic code is as follows:

 
 
  1. Import java. io. IOException;
  2. Import java. io. InputStream;
  3. Import java. io. OutputStream;
  4. Import java. io. OutputStreamWriter;
  5. Import java. util. Properties;
  6.  
  7. /**
  8. * When exporting data, pay attention to the running environment of command statements. If you have added the bin in the mysql installation path
  9. * In the path variable of the system, you can directly use the command statement during export. Otherwise, you must execute the command statement
  10. * Add the path where the command is located, that is, the mysqldump command under the bin in the mysql installation path.
  11. * @ Author andy
  12. *
  13. */
  14. Public class MySqlImportAndExport {
  15.  
  16. Public static void main (String args []) throws IOException {
  17. InputStream is = MySqlImportAndExport. class. getClassLoader (). getResourceAsStream ("jdbc. properties ");
  18. Properties properties = new Properties ();
  19. Properties. load (is );
  20. // MySqlImportAndExport. export (properties); // I will directly throw an exception
  21. MySqlImportAndExport. importSql (properties );
  22. }
  23. /**
  24. * Export the specified database at the specified location to the specified location based on the configuration of the property File
  25. * @ Param properties
  26. * @ Throws IOException
  27. */
  28. Public static void export (Properties properties) throws IOException {
  29. Runtime runtime = Runtime. getRuntime ();
  30. String command = getExportCommand (properties );
  31. Runtime.exe c (command); // if there is a simple exception, I will directly throw it
  32. }
  33. /**
  34. * Import the specified file content at the specified location to the specified database based on the configuration of the property file.
  35. * Mysql Database Import in the Command window is generally completed in three steps:
  36. * The first step is to log on to mysql; mysql-uusername-ppassword-hhost-Pport-DdatabaseName; if the database name is specified during login
  37. * Directly go to the database, so that you can skip step 2 and step 3;
  38. * Step 2: switch to the imported target database; use importDatabaseName;
  39. * Step 3: import data from the target file to the target database; source importPath;
  40. * @ Param properties
  41. * @ Throws IOException
  42. */
  43. Public static void importSql (Properties properties) throws IOException {
  44. Runtime runtime = Runtime. getRuntime ();
  45. // Because mysql Database Import in the Command window is generally divided into three steps, the executed command will appear in the form of a string array
  46. String cmdarray [] = getImportCommand (properties); // obtain the commands required for Database Import Based on the configuration of the attribute file to form an array
  47. // Runtime.exe c (cmdarray); // It is also simple to directly throw an exception.
  48. Process process = runtime.exe c (cmdarray [0]);
  49. // After the first command is executed, you have logged on to mysql. Therefore, the mysql Command window is used.
  50. // Code after Process Execution
  51. OutputStream OS = process. getOutputStream ();
  52. OutputStreamWriter writer = new OutputStreamWriter (OS );
  53. // Execute commands 1 and 2 together
  54. Writer. write (cmdarray [1] + "\ r \ n" + cmdarray [2]);
  55. Writer. flush ();
  56. Writer. close ();
  57. OS. close ();
  58. }
  59. /**
  60. * Assemble command statements using the configuration provided by the property File
  61. * Note the following when assembling command statements: Generally, you can directly use the command in the Command window
  62. * When exporting data, you can simply use ">" to indicate where the data is exported, that is, mysqldump-uusername-ppassword databaseName> exportPath,
  63. * But in Java, writing like this won't work. You need to use-r to explicitly specify the export location, for example:
  64. * Mysqldump-uusername-ppassword databaseName-r exportPath.
  65. * @ Param properties
  66. * @ Return
  67. */
  68. Private static String getExportCommand (Properties properties ){
  69. StringBuffer command = new StringBuffer ();
  70. String username = properties. getProperty ("jdbc. username"); // User Name
  71. String password = properties. getProperty ("jdbc. password"); // User password
  72. String exportDatabaseName = properties. getProperty ("jdbc. exportDatabaseName"); // name of the database to be exported
  73. String host = properties. getProperty ("jdbc. host"); // the host from which the database is exported. If this value is not specified, localhost is used by default.
  74. String port = properties. getProperty ("jdbc. port"); // The port number used
  75. String exportPath = properties. getProperty ("jdbc. exportPath"); // export path
  76. // Pay attention to spaces and spaces
  77. Command. append ("mysqldump-u "). append (username ). append ("-p "). append (password) // The password is a small p, while the port is a large P.
  78. . Append ("-h "). append (host ). append ("-P "). append (port ). append (""). append (exportDatabaseName ). append ("-r "). append (exportPath );
  79. Return command. toString ();
  80. }
  81. /**
  82. * According to the configuration of the attribute file, the command required to import data from the target file to the target database is obtained in three steps.
  83. * If the database name is specified during login
  84. * Directly go to the database, so that you can skip step 2 and step 3;
  85. * @ Param properties
  86. * @ Return
  87. */
  88. Private static String [] getImportCommand (Properties properties ){
  89. String username = properties. getProperty ("jdbc. username"); // User Name
  90. String password = properties. getProperty ("jdbc. password"); // password
  91. String host = properties. getProperty ("jdbc. host"); // host of the imported target database
  92. String port = properties. getProperty ("jdbc. port"); // The port number used
  93. String importDatabaseName = properties. getProperty ("jdbc. importDatabaseName"); // name of the imported target database
  94. String importPath = properties. getProperty ("jdbc. importPath"); // the location of the imported target file
  95. // Step 1: Obtain the logon command statement
  96. String loginCommand = new StringBuffer (). append ("mysql-u "). append (username ). append ("-p "). append (password ). append ("-h "). append (host)
  97. . Append ("-P"). append (port). toString ();
  98. // Step 2: Obtain the command statement for switching the database to the target database
  99. String switchCommand = new StringBuffer ("use"). append (importDatabaseName). toString ();
  100. // Step 3: Obtain the imported command statement
  101. String importCommand = new StringBuffer ("source"). append (importPath). toString ();
  102. // Array of command statements to be returned
  103. String [] commands = new String [] {loginCommand, switchCommand, importCommand };
  104. Return commands;
  105. }
  106. }

The jdbc. properties file used above

 
 
  1. jdbc.username=root  
  2. jdbc.password=password 
  3. jdbc.host=localhost  
  4. jdbc.port=3306  
  5. jdbc.exportDatabaseName=dbName  
  6. jdbc.exportPath=d\:\\dbName.sql  
  7. jdbc.importDatabaseName=test  
  8. jdbc.importPath=d\:\\dbName.sql 

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.