1 PackageCom.learn4j.bat;2 3 Public classBackup {4 PrivateString user_name;//Database user name5 PrivateString USER_PSW;//Database Password6 PrivateString db_name;//the name of the database that needs to be backed up7 PrivateString host_ip;//Host IP8 PrivateString User_charset;//Character Set9 PrivateString Backup_path;//the path where the backup file is storedTen PrivateString stmt;//Command One A PublicBackup (String user_name, String user_psw, String db_name, - string host_ip, String user_charset, String backup_path) { - This. User_name =user_name; the This. USER_PSW =USER_PSW; - This. db_name =db_name; - //host IP; - if(Host_ip = =NULL|| Host_ip.equals ("")) + This. host_ip = "localhost";//default is native - Else + This. host_ip =host_ip; A //Character Set at if(User_charset = =NULL|| User_charset.equals ("")) - This. User_charset = "";//The default is the set of character sets at installation time - Else - This. User_charset = "--default-character-set=" +User_charset; - This. Backup_path =Backup_path; - This. stmt = "C:\\wamp\\bin\\mysql\\mysql5.5.20\\bin\\mysqldump" in+ This. db_name + "-H" + This. host_ip + "-u" + This. User_name -+ "-P" + This. USER_PSW + This. User_charset + "--result-file=" to+ This. Backup_path; + } - the Public BooleanBackup_run () { * BooleanRun_result =false; $ Try {Panax NotoginsengRuntime.getruntime (). EXEC ( This. stmt); -Run_result =true; the}Catch(Exception e) { + e.printstacktrace (); A } the returnRun_result; + } - $ Public Static voidMain (string[] args) { $Backup backup =NewBackup ("root", "123456", "Student",NULL, "UTF8", -"D:\\test.sql"); - Booleanresult =Backup.backup_run (); the if(Result) -SYSTEM.OUT.PRINTLN ("Backup succeeded");Wuyi } the}
Original link: http://blog.sina.com.cn/s/blog_59ae45de0100ds17.html
Java automatic backup MySQL database (reproduced)