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)