Import java. io. IOException;
Import java. text. SimpleDateFormat;
Import java. util. Date;
Public class BackupMysql {
/** Access the url of the MySQL database server */
Private String serverUrl;
/** Username used to access the MySQL database */
Private String username;
/** Password used to access the MySQL database */
Private String password;
Public String getServerUrl (){
Return serverUrl;
}
Public void setServerUrl (String serverUrl ){
This. serverUrl = serverUrl;
}
Public String getUsername (){
Return username;
}
Public void setUsername (String username ){
This. username = username;
}
Public String getPassword (){
Return password;
}
Public void setPassword (String password ){
This. password = password;
}
Public BackupMysql (String serverUrl, String username, String password ){
Super ();
This. serverUrl = serverUrl;
This. username = username;
This. password = password;
}
Public String backup (String backupPath, String dbName) throws IOException {
String backupFile = backupPath + dbName + new SimpleDateFormat ("yyyy-MM-dd-HH-mm-ss"). format (new Date () + ". SQL ";
String mysql = "mysqldump" + "-- host =" + serverUrl + "-- user =" + username + "-- password =" + password + "-- opt" + dbName + "> "+ backupFile;
Java.lang.runtime.getruntime(cmd.exe c ("cmd/c" + mysql );
System. out. println ("backup successful! ");
Return backupFile;
}
Public void restore (String restoreFile, String dbName) throws Exception {
String mysql = "mysql" + "-h" + serverUrl + "-u" + username + "-p" + password + "" + dbName + "<" + restoreFile;
System. out. println (mysql );
Java.lang.runtime.getruntime(cmd.exe c ("cmd/c" + mysql );
System. out. println ("restored successfully! ");
}
/**
* @ Param args
*/
Public static void main (String [] args ){
String serverUrl = "127.0.0.1 ";
String userName = "root ";
String pwd = "root ";
BackupMysql backup = new BackupMysql (serverUrl, userName, pwd );
Try {
Backup. restore ("d:/nationz2010-12-20-14-16-47. SQL", "nationz ");
} Catch (Exception e ){
E. printStackTrace ();
}
}
}
From the column gdn_wolf