Java implementation MySQL database backup

Source: Internet
Author: User

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

Import Java.io.bufferedreader;import java.io.file;import java.io.fileoutputstream;import java.io.IOException; Import java.io.inputstreamreader;import java.io.outputstreamwriter;import java.io.printwriter;/** * MySQL Database backup * * @ Author Gaohuanjie */public class Mysqldatabasebackup {/** * Java code Implementation MySQL database export * * @author Gaohuanjie * @param hostip MyS QL Database server Address IP * @param userName the username required to enter the database * @param userName the user name required to enter the database * @param password The password required to enter the database * @param Savepa Th 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 returns false. */public Static Boolean Exportdatabasetool (String HostIP, String userName, string password, string savepath, String Filena Me, String databaseName) throws interruptedexception {file SaveFile = new File (Savepath); if (!savefile.exists ()) {//If the directory does not exists savefile.mkdirs ();//Create Folder}if (!savepath.endswith (file.separator)) {Savepath = Savepath + file.separator;} PrintWriter printwriter = null; BufferedReader BufferedReader = Null;try {printwriter = new PrintWriter (new OutputStreamWriter (New FileOutputStream (Savepath + fileName), "UTF8")); Process process = Runtime.getruntime (). EXEC ("mysqldump-h" + HostIP + "-u" + userName + "-P" + Password + "--set-chars Et=utf8 "+ databaseName); InputStreamReader inputstreamreader = new InputStreamReader (Process.getinputstream ()," UTF8 "            ); BufferedReader = new BufferedReader (InputStreamReader);            String Line;            while (line = Bufferedreader.readline ())! = null) {printwriter.println (line); } printwriter.flush (); if (process.waitfor () = = 0) {//0 indicates normal termination of the thread. return true;}} catch (IOException e) {e.printstacktrace ();} finally {try {if (BufferedReader! = null) {Bufferedreader.close ();} if (printwriter! = null) {Printwriter.close ();}} catch (IOException e) {e.printstacktrace ();}} return false;} public static void Main (string[] args) {try {if (Exportdatabasetool ("172.16.0.127", "root", "123456", "d:/backupdatabase "," 2014-10-14.sql "," Test ")) {SystEM.OUT.PRINTLN ("Database successfully backed up!!! ");} else {System.out.println ("Database backup failed!!! ");}} catch (Interruptedexception e) {e.printstacktrace ();}}}

Java implementation MySQL database backup

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.