Rights statement: This article for Bo Master original article, without Bo Master permission not reproduced.
Blog "Java implementation MySQL database backup (i)" using I/O stream to implement the MySQL database backup, this method is more complicated, the following describes another way to back up the MySQL database:
[Java]View PlainCopy
- 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 is located on the server address IP
- * @param userName User name required to enter the database
- * @param password The password required to enter the database
- * @param savepath Database Export File save path
- * @param filename Export file name of the filename database
- * @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, St Ring FileName, String databaseName) {
- File SaveFile = new File (Savepath);
- if (!savefile.exists ()) {//If the directory does not exist
- Savefile.mkdirs (); //Create a folder
- }
- if (!savepath.endswith (File.separator)) {
- Savepath = Savepath + file.separator;
- }
- StringBuilder StringBuilder = new StringBuilder ();
- 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 {
- 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)