MySQL database backup in Java (2) _ MySQL

Source: Internet
Author: User
The blog "Java implements MySQL database backup (I)" uses the IO stream method to back up the MySQL database. this method is complicated. the following introduces another method to back up the MySQL database: importjavaioFile; importjavaioIOExcept blog 《 Implement MySQL database backup in Java (1)I/O stream is used to back up the MySQL database. this method is complicated. the following introduces another method to back up the MySQL database:

Import java. io. file; import java. io. IOException; /*** MySQL database backup *** @ author GaoHuanjie */public class MySQLDatabaseBackup {/*** Java code for MySQL database export ** @ author GaoHuanjie * @ param hostIP MySQL database server IP address * @ param userName the userName required to enter the database * @ param password The password required to enter the database * @ param savePath database export file storage path * @ param fileName database export file name * @ param databaseName name of the database to be exported * @ return returns true, indicating that the export is successful, otherwise, false is returned. */Public static boolean exportDatabaseTool (String hostIP, String userName, String password, String savePath, String fileName, String databaseName) {File saveFile = new File (savePath); if (! SaveFile. exists () {// if the directory does not exist, saveFile. mkdirs (); // create a folder} if (! SavePath. endsWith (File. separator) {savePath = savePath + File. separator;} StringBuilder stringBuilder = new StringBuilder (); stringBuilder. append ("mysqldump "). append ("-- opt "). append ("-h "). append (hostIP); stringBuilder. append ("-- user = "). append (userName ). append ("-- password = "). append (password ). append ("-- lock-all-tables = true"); stringBuilder. append ("-- result-file = "). append (savePath + fileName ). appe Nd ("-- default-character-set = utf8 "). append (databaseName); try {Process process = runtime.getruntime(.exe c (stringBuilder. toString (); if (process. waitFor () = 0) {// 0 indicates that the thread is terminated normally. Return true ;}} catch (IOException e) {e. printStackTrace ();} catch (InterruptedException e) {e. printStackTrace ();} return false;} public static void main (String [] args) throws InterruptedException {if (exportDatabaseTool ("172.16.0.127", "root", "123456 ", "D:/backupDatabase", "2014-10-14. SQL", "test") {System. out. println ("database backup successful !!! ");} Else {System. out. println (" database backup failed !!! ");}}}

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.