Put it on first. Reference address: http://www.open-open.com/lib/view/open1331354269624.html
Direct Sticker Code:
PackageCom.ly.jxc.util;Importjava.io.IOException;ImportJava.text.SimpleDateFormat;Importjava.util.Date; Public classDbrecover {/**the URL where the MySQL database server is accessed*/ PrivateString ServerURL; /**user name to access MySQL database*/ PrivateString username; /**password to access MySQL database*/ PrivateString password; PublicString Getserverurl () {returnServerURL; } Public voidSetserverurl (String serverurl) { This. ServerURL =ServerURL; } PublicString GetUserName () {returnusername; } Public voidSetusername (String username) { This. Username =username; } PublicString GetPassword () {returnpassword; } Public voidSetPassword (String password) { This. Password =password; } Publicdbrecover (String serverurl,string Username, string password) {Super(); This. serverurl=ServerURL; This. Username =username; This. Password =password; } PublicString Backup (String backuppath, String dbName)throwsIOException {String backupFile= backuppath+NewSimpleDateFormat ("Yyyy-mm-dd-hh-mm-ss"). Format (NewDate ()) + ". sql"; String MySQL=backuppath+ "mysqldump" + "--host=" +serverurl+ "--user=" + username + "--password=" + password + "--opt" + DbName + "> "+BackupFile; System.out.println ("Backup" +MySQL); Java.lang.Runtime.getRuntime (). EXEC ("CMD/C" +MySQL); System.out.println ("Backup succeeded!"); returnBackupFile; } Public voidRestore (String restorefile, string dbname,string path)throwsException {String mysql= path+ "MySQL" + "-H" +serverurl+ "-u" + Username + "-P" + Password + "" + DbName + "<" +Restorefile; System.out.println ("+++++++++++++++++++++++++++"+MySQL); Java.lang.Runtime.getRuntime (). EXEC ("CMD/C" +MySQL); System.out.println ("Restore succeeded!"); } /** * @paramargs*/ Public Static voidMain (string[] args) {String ServerURL= "192.168.1.6"; String UserName= "Root"; String pwd= "Root"; Dbrecover Backup=Newdbrecover (Serverurl,username, PWD); Try{backup.backup ("", "LYJXC");//Backup.restore ("D:/nationz2010-12-20-14-16-47.sql", "Nationz");}Catch(Exception e) {e.printstacktrace (); } }}
Note the point: 1. I have some changes in the execution of backup and restore, because I need to use the Mysql.exe,mysqldump.exe under MySQL bin, because I have not configured the environment settings for MySQL, so I copy these two files to my project exec This is done by executing the two files in my project. The two variables in the code that are in front of MySQL are the mysql.exe,mysqldump.exe that represent my project.
2. The purpose of the copy is another, because the MySQL default installation path is under the program file folder of the system disk. If directly with Mysql>bin below Mysql.exe,mysqldump.exe, backed up SQL file is 0kb, because the program file folder name has a space, will affect . So to insure I put the file in my project to execute.
3. There seems to be no ":" In the name of the SQL file, I want to use a colon, the result is not produced.
OK to this end. I locally tested the internal server through.
Database backup and recovery in a Java Web project