- Usually in the MySQL database backup and recovery, most of the use of CMD in the implementation of the MySQL command to implement.
For example:
Mysqldump-h127.0.0.1-uroot-ppass Test > D:/test.sql ---Back up test database to D drive
Mysql-h127.0.0.1-uroot-ppass test< test.sql ---to restore the database script of D backup to the database
More commands See: http://www.cnblogs.com/xcxc/archive/2013/01/30/2882840.html
- Today, Java code is used to implement database backup.
-
- In the cmd call command line, is actually called the MySQL installation path below the bin directory below the Msqldump.exe and Mysql.exe to complete the corresponding work
- So, in Java code, we also need to do backup and restore work by calling Mysqldump.exe and Mysql.exe
- Runtime.getruntime (). EXEC (String args), Java calls the external software EXE to execute the command API, see: http://www.cnblogs.com/tohxyblog/p/6501396.html
- Database backup specific code
/** * @paramhostip IP address, can be native or remote *@paramuser name of the userName database *@paramPassword for password database *@paramSavepath Path to backup *@paramfilename of the filename backup *@paramdatabaseName Name of the database to be backed up *@return */ Public Static BooleanBackup (String hostip, String userName, string password, string savepath, String fileName, String database Name) {FileName+ = ". sql"; File SaveFile=NewFile (Savepath); if(!savefile.exists ()) {//If the directory does not existSavefile.mkdirs ();//Create a folder } if(!Savepath.endswith (File.separator)) {Savepath= Savepath +File.separator; } //commands for stitching command linesStringBuilder StringBuilder =NewStringBuilder (); 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 { //calling external execution exe file JAVAAPIProcess Process =runtime.getruntime (). EXEC (stringbuilder.tostring ()); if(process.waitfor () = = 0) {//0 indicates that the thread terminated normally. return true; } } Catch(IOException e) {e.printstacktrace (); } Catch(interruptedexception e) {e.printstacktrace (); } return false; }
/** * @paramfilepath script path for database backup *@paramIP IP Address *@paramDatabase name *@paramuserName User name *@paramPassword Password *@return */ Public Static BooleanRecover (String filepath,string ip,string database, string username,string password) {string STMT1= "Mysqladmin-h" +ip+ "-U" +username+ "-P" +password+ "create" +database; String stmt2= "Mysql-h" +ip+ "-U" +username+ "-P" +password+ "" +database+ "<" +filepath; string[] cmd= {"cmd", "/C", stmt2}; Try{runtime.getruntime (). exec (STMT1); Runtime.getruntime (). exec (CMD); System.out.println ("Data has been imported into the database from" + filepath + "); } Catch(IOException e) {e.printstacktrace (); return false; } return true; }
All the above for today's sharing, if you need to know more in-depth knowledge,
Please enter the Cicada Hall community: http://www.zhiliaotang.com/portal.php;
Reprint please indicate the source;
Please give us a lot of advice! Welcome to advise, non-sincere do not Disturb!!!
---by get_chen
[Cicada Hall Learning note]_java code to implement backup and restore of MySQL database