Java implementation MySQL database backup (ii)

Source: Internet
Author: User

Blog "" Using I/O streaming way to achieve the MySQL database backup, this method is more complicated, the following describes another way to back up the MySQL database:

Import java.io.file;import java.io.ioexception;/** * MySQL Database backup * * @author Gaohuanjie */public class Mysqldatabasebackup {/** * Java code Implementation MySQL database export * * @author Gaohuanjie * @param hostip mysql database server address IP * @param userName the user name required to enter the database * @par  Am UserName the username required to enter the database * @param password The password required to enter the database * @param savepath Database Export File save path * @param filename Database Export file name * @param DatabaseName the name of the database to be exported * @return returns True to indicate that the export was successful, otherwise false. */public Static Boolean Exportdatabasetool (String HostIP, String userName, string password, string savepath, String Filena Me,string databaseName) {file SaveFile = new File (Savepath), if (!savefile.exists ()) {//If the directory does not exist Savefile.mkdirs ();/ Create Folder}if (!savepath.endswith (file.separator)) {Savepath = Savepath + file.separator;} StringBuilder StringBuilder = new StringBuilder () stringbuilder.append ("Mysqldump"). 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). Append ("--default-character-set=utf8"). Append (databaseName); try {process process = Runtime.getruntime (). EXEC (stringbuilder.tostring ()); if (process.waitfor () = = 0) {//0 indicates normal termination of the thread. 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 succeeded!!! ") ");} else {System.out.println ("Database backup failed!!! ");}}}


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.