[Cicada Hall Learning note]_java code to implement backup and restore of MySQL database

Source: Internet
Author: User

    • Usually in the MySQL database backup and recovery, most of the use of CMD in the implementation of the MySQL command to implement.

For example:

Mysqldump-h127.0.0.1-uroot-ppass Test > D:/test.sql  ---Back up test database to D drive
Mysql-h127.0.0.1-uroot-ppass test< test.sql  ---to restore the database script of D backup to the database

More commands See: http://www.cnblogs.com/xcxc/archive/2013/01/30/2882840.html
    • Today, Java code is used to implement database backup.
        • In the cmd call command line, is actually called the MySQL installation path below the bin directory below the Msqldump.exe and Mysql.exe to complete the corresponding work
        • So, in Java code, we also need to do backup and restore work by calling Mysqldump.exe and Mysql.exe
      • Runtime.getruntime (). EXEC (String args), Java calls the external software EXE to execute the command API, see: http://www.cnblogs.com/tohxyblog/p/6501396.html

    • Database backup specific code
/**     * @paramhostip IP address, can be native or remote *@paramuser name of the userName database *@paramPassword for password database *@paramSavepath Path to backup *@paramfilename of the filename backup *@paramdatabaseName Name of the database to be backed up *@return     */     Public Static BooleanBackup (String hostip, String userName, string password, string savepath, String fileName, String database Name) {FileName+ = ". sql"; File SaveFile=NewFile (Savepath); if(!savefile.exists ()) {//If the directory does not existSavefile.mkdirs ();//Create a folder        }        if(!Savepath.endswith (File.separator)) {Savepath= Savepath +File.separator; }        //commands for stitching command linesStringBuilder StringBuilder =NewStringBuilder (); 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). Append ("--default-character-set=utf8"). Append (DatabaseName); Try {            //calling external execution exe file JAVAAPIProcess Process =runtime.getruntime (). EXEC (stringbuilder.tostring ()); if(process.waitfor () = = 0) {//0 indicates that the thread terminated normally.                 return true; }        } Catch(IOException e) {e.printstacktrace (); } Catch(interruptedexception e) {e.printstacktrace (); }        return false; }
    • Database Recovery Code
/**     * @paramfilepath script path for database backup *@paramIP IP Address *@paramDatabase name *@paramuserName User name *@paramPassword Password *@return     */     Public Static BooleanRecover (String filepath,string ip,string database, string username,string password) {string STMT1= "Mysqladmin-h" +ip+ "-U" +username+ "-P" +password+ "create" +database; String stmt2= "Mysql-h" +ip+ "-U" +username+ "-P" +password+ "" +database+ "<" +filepath; string[] cmd= {"cmd", "/C", stmt2}; Try{runtime.getruntime (). exec (STMT1);            Runtime.getruntime (). exec (CMD); System.out.println ("Data has been imported into the database from" + filepath + "); } Catch(IOException e) {e.printstacktrace (); return false; }        return true; }

All the above for today's sharing, if you need to know more in-depth knowledge,

Please enter the Cicada Hall community: http://www.zhiliaotang.com/portal.php;

Reprint please indicate the source;
Please give us a lot of advice! Welcome to advise, non-sincere do not Disturb!!!
---by get_chen

[Cicada Hall Learning note]_java code to implement backup and restore of MySQL database

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.