您的位置:首页 > 编程语言 > Java开发

Java 备份 还原 MySQL

2010-11-18 16:44 302 查看
public static void backup() {
// 数据库导出
String user = "xiaohuan"; // 数据库帐号
String password = "xiaohuanjie"; // 登陆密码
String database = "srms"; // 需要备份的数据库名
String filepath = "d:/xiao.sql"; // 备份的路径地址

// 注意mysqldump是调用mysql数据库的一个组件,在未在系统变量中声明的话,要在这里写mysqldump的完整路径
String stmt1 = "mysqldump " + database + " -u " + user + " -p"
+ password + " --result-file=" + filepath;

try {
Runtime.getRuntime().exec(stmt1);
} catch (IOException e) {
e.printStackTrace();
}
}

public static void load() {
String filepath = "d:/xiao.sql"; // 备份的路径地址

// 新建数据库srms
String stmt1 = "mysqladmin -u xiaohuan -pxiaohuanjie create srms";

String stmt2 = "mysql -u xiaohuan -pxiaohuanjie srms < " + filepath;
String[] cmd = { "cmd", "/c", stmt2 };

try {
Runtime.getRuntime().exec(stmt1);
Runtime.getRuntime().exec(cmd);
} catch (IOException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {

// backup();

load();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: