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