Java 实现 MySQL 数据库 备份与还原
1.MySQL_Backup程序清单:
package DB;
import java.io.IOException;
/**
* MySQL数据库备份
* @author xqh
*
*/
public class MySQL_Backup {
public static void main(String[] args) {
try {
String str = "mysqldump -uroot -p1120 db_test > d:/db_test.sql"; // 注意命令格式
Runtime rt=Runtime.getRuntime();
rt.exec("cmd /c "+str);
System.out.println("备份成功!");
} catch (IOException e) {
System.out.println("备份失败!");
e.printStackTrace();
}
}
}
2.MySQL_Recover程序清单:
package DB;
import java.io.IOException;
/**
* MySQL数据库还原
* @author x
*
*/
public class MySQL_Recover {
public static void main(String[] args) {
String filepath = "d:\\db_test.sql";
String stmt = "mysql -uroot -p1120 db_test < " + filepath;
try {
Runtime.getRuntime().exec("cmd /c " + stmt);
System.out.println("还原成功!");
} catch (IOException e) {
System.out.println("还原失败!");
e.printStackTrace();
}
}
}