javaweb实现mysql备份功能_java实现web应用(备份还原数据库)

----------------------------------------------------->>>>>>>>

原理:MySQL 数据库备份原理: Navicat等数据库界面软件通用的数据库备份原理就是直

接调用MYSQL本身的系统命令。

MySQL本身的系统命令:

--opt –h localhost --user=root  --password=admin --lock-all-tables=true --result-file=E://oes//2221.sql --default-character-set=utf8 oes

解析:主机–h,用户名--user,密码—password,锁定所有表--lock-all-tables=true,

目标文件--result-file,编码--default-character-set=utf8,数据源oes

Java中执行系统命令方法:

Runtime cmd = Runtime.getRuntime();

Process p = cmd.exec(“”);   //执行CMD指令(String)

由于无法确定主机是否配置了MySQL环境变量,所以需要最保险的确定MySQL中mysqldump的位置,它存在于MySQL安装文件夹得Bin目录下,问题就是如何获取MySQL的安装目录?

----------------------------------------------------->>>>>>>>

针对于获取MySQL的安装目录,我用的是比较笨的方法:解析注册表。

找到注册表中MySQL的软件信息,里面包含有软件的安装地址,卸载地址,版本号等等基本信息,直接取用它的安装信息就行。

软件信息在注册表中的位置:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\

软件关联注册表中软件信息:

----------------------------------------------------->>>>>>>>

类CheckSoftware,解析MySQL软件安装地址

importjava.io.BufferedReader;

importjava.io.IOException;

importjava.io.InputStreamReader;

public classCheckSoftware {

/*

*遍历注册表,查询MySQL的注册表关联

*/

public staticString check()throwsException {

Runtime runtime = Runtime.getRuntime();

Process process =null;

process = runtime

.exec("cmd /c reg query HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\");

BufferedReader in =newBufferedReader(newInputStreamReader(

process.getInputStream()));

String string =null;

while((string = in.readLine()) !=null) {

process = runtime.exec("cmd /c reg query "+ string

+" /v DisplayName");

BufferedReader name =newBufferedReader(newInputStreamReader(

process.getInputStream()));

String message = queryValue(string,"DisplayName");

if(message !=null&& message.contains("MySQL")) {

String message2 = queryValue(string,"InstallLocation");

returnmessage2;

}

}

in.close();

process.destroy();

return null;

}

/*

*查询出需要的MySQL服务的安装路径

*/

private staticString queryValue(String string, String method)

throwsIOException {

String pathString ="";

Runtime runtime = Runtime.getRuntime();

Process process =null;

BufferedReader br =null;

process = runtime.exec("cmd /c reg query "+ string +" /v "+ method);

br =newBufferedReader(newInputStreamReader(process.getInputStream()));

br.readLine();

br.readLine();//去掉前两行无用信息

if((pathString = br.readLine()) !=null) {

pathString = pathString.replaceAll(method +"    REG_SZ    ","");//去掉无用信息

returnpathString;

}

returnpathString;

}

}

----------------------------------------------------->>>>>>>>

类JavaMysql备份还原数据库

importjava.io.File;

importjava.io.IOException;

importjava.io.InputStream;

importjava.util.Properties;

public classJavaMysql {

/*

*备份数据库1、读取配置文件2、启动智能查询Mysql安装目录3、备份数据库为sql文件

*/

public static voidbackup(String sql) {

Properties pros = getPprVue("prop.properties");

String username = pros.getProperty("username");

String password = pros.getProperty("password");

CheckSoftware c =null;

try{

System.out.println("MySQL服务安装地址 :"+c.check().toString());

}catch(Exception e2) {

e2.printStackTrace();

}

String mysqlpaths;

try{

mysqlpaths = c.check().toString() +"bin"+"\\";

String databaseName = pros.getProperty("databaseName");

String address = pros.getProperty("address");

String sqlpath = pros.getProperty("sql");

File backupath =newFile(sqlpath);

if(!backupath.exists()) {

backupath.mkdir();

}

StringBuffer sb =newStringBuffer();

sb.append(mysqlpaths);

sb.append("mysqldump ");

sb.append("--opt ");

sb.append("-h ");

sb.append(address);

sb.append(" ");

sb.append("--user=");

sb.append(username);

sb.append(" ");

sb.append("--password=");

sb.append(password);

sb.append(" ");

sb.append("--lock-all-tables=true ");

sb.append("--result-file=");

sb.append(sqlpath);

sb.append(sql);

sb.append(" ");

sb.append("--default-character-set=utf8 ");

sb.append(databaseName);

System.out.println("cmd指令 :"+sb.toString());

Runtime cmd = Runtime.getRuntime();

try{

Process p = cmd.exec(sb.toString());

}catch(IOException e) {

e.printStackTrace();

}

}catch(Exception e1) {

e1.printStackTrace();

}

}

/*

*读取属性文件

*/

public staticProperties getPprVue(String properName) {

InputStream inputStream = JavaMysql.class.getClassLoader()

.getResourceAsStream(properName);

Properties p =newProperties();

try{

p.load(inputStream);

inputStream.close();

}catch(IOException e) {

e.printStackTrace();

}

returnp;

}

/*

*根据备份文件恢复数据库

*/

public static voidload(String filename) {

Properties pros = getPprVue("prop.properties");

String root = pros.getProperty("jdbc.username");

String pass = pros.getProperty("jdbc.password");

String mysqlpaths = c.check().toString() +"bin"+"\\";

String sqlpath = pros.getProperty("sql");

String filepath = mysqlpaths + sqlpath + filename;//备份的路径地址

String stmt1 = mysqlpaths +"mysqladmin -u "+ root +" -p"+ pass

+" create finacing";// -p后面加的是你的密码

String stmt2 = mysqlpaths +"mysql -u "+ root +" -p"+ pass

+" finacing < "+ filepath;

String[] cmd = {"cmd","/c", stmt2 };

try{

Runtime.getRuntime().exec(stmt1);

Runtime.getRuntime().exec(cmd);

System.out.println("数据已从"+ filepath +"导入到数据库中");

}catch(IOException e) {

e.printStackTrace();

}

}

/*

* Test测试

*/

public static voidmain(String[] args)throwsIOException {

backup("2221.sql");

}

}

----------------------------------------------------->>>>>>>>

属性文件:prop.properties,动态配置用户名及密码等基本属性

username =root

password =admin

sql =E://oes//

address=localhost

posted on 2012-09-07 10:08 Nirvana reborn 阅读(5117) 评论(6)  编辑  收藏 所属分类: ★开发经验

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值