在JSP页面调用JAVA方法实现MySQL数据库的备份和恢复
2012-11-29 09:41
901 查看
今天弄了好久,终于搞定了这个问题。使用java代码实现数据库的备份。
Java代码代码
package cn.qm.db;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class Command {
/*
public static void main(String[] args) throws IOException {
Command com = new Command();
com.backupDatebase("localhost","root","root", "JXC", "D:/jxc.sql");
}
/**
* 执行dos命令
* @param cmd
* @return
*/
public String execCmd(String cmd) {
StringBuffer sb = new StringBuffer("");
StringBuffer str = new StringBuffer();
str.append("cmd.exe /c \"").append(cmd).append("\"");
System.out.println(str); //打印执行的命令
Process ls_proc;
try {
ls_proc = Runtime.getRuntime().exec(str.toString());
BufferedReader in = new BufferedReader(
new InputStreamReader(
new DataInputStream(ls_proc.getInputStream())));
String ss = "";
while((ss = in.readLine()) != null) {
sb.append(ss).append("\n");
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
/**
* 执行mysql数据库备份
* @param ip
* @param username
* @param password
* @param datebaseName
* @param filePath
* @return
*/
public boolean backupDatebase(String ip, String username, String password,String datebaseName, String filePath) {
String strCommand = "mysqldump -h "+ip+" -u" + username + " -p" + password + " " + datebaseName + " > " + filePath;
String result = execCmd(strCommand);
System.out.println(result);
return true;
}
/**
* 根据返回结果验证是否成功
* @param result
* @return
*/
public boolean check(String result) {
return true;
}
}
在JSP页面只要调用这个JAVA类就可以。(文件名只能是.sql)
Html代码代码
<%@ page language="java" import="java.util.*,cn.qm.db.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>数据库备份测试</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
Command com = new Command();
String ip = "localhost";//ip地址
String username = "root";//MySQL数据库的用户名
String password = "root";//MySQL数据库的密码
String database = "JXC";//数据库名字
String url = "D:/jxc.sql";//备份的目的地址
boolean check = com.backupDatebase(ip,username,password,database,url);
if(check){
%>
数据库备份成功
<%} %>
</body>
</html>
下面是恢复数据的代码。
Java代码代码
package cn.qm.db;
import java.io.*;
import java.lang.*;
/*
* 还原MySql数据库
* */
public class Recover {
public boolean load(){
String filepath = "d:\\jxc.sql"; // 备份的路径地址
//新建数据库test
String stmt1 = "mysqladmin -u root -proot create jxctest";
String stmt2 = "mysql -u root -proot jxctest < " + filepath;
String[] cmd = { "cmd", "/c", stmt2 };
try {
Runtime.getRuntime().exec(stmt1);
Runtime.getRuntime().exec(cmd);
System.out.println("数据已从 " + filepath + " 导入到数据库中");
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
}
Html代码代码
<%@ page language="java" import="java.util.*,cn.qm.db.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>数据恢复测试</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
Recover com = new Recover();
String url = "D:/jxc.sql";
boolean check = com.load();
if(check){
%>
数据库恢复成功
<%} %>
</body>
</html>
Java代码代码
package cn.qm.db;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class Command {
/*
public static void main(String[] args) throws IOException {
Command com = new Command();
com.backupDatebase("localhost","root","root", "JXC", "D:/jxc.sql");
}
/**
* 执行dos命令
* @param cmd
* @return
*/
public String execCmd(String cmd) {
StringBuffer sb = new StringBuffer("");
StringBuffer str = new StringBuffer();
str.append("cmd.exe /c \"").append(cmd).append("\"");
System.out.println(str); //打印执行的命令
Process ls_proc;
try {
ls_proc = Runtime.getRuntime().exec(str.toString());
BufferedReader in = new BufferedReader(
new InputStreamReader(
new DataInputStream(ls_proc.getInputStream())));
String ss = "";
while((ss = in.readLine()) != null) {
sb.append(ss).append("\n");
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
/**
* 执行mysql数据库备份
* @param ip
* @param username
* @param password
* @param datebaseName
* @param filePath
* @return
*/
public boolean backupDatebase(String ip, String username, String password,String datebaseName, String filePath) {
String strCommand = "mysqldump -h "+ip+" -u" + username + " -p" + password + " " + datebaseName + " > " + filePath;
String result = execCmd(strCommand);
System.out.println(result);
return true;
}
/**
* 根据返回结果验证是否成功
* @param result
* @return
*/
public boolean check(String result) {
return true;
}
}
在JSP页面只要调用这个JAVA类就可以。(文件名只能是.sql)
Html代码代码
<%@ page language="java" import="java.util.*,cn.qm.db.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>数据库备份测试</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
Command com = new Command();
String ip = "localhost";//ip地址
String username = "root";//MySQL数据库的用户名
String password = "root";//MySQL数据库的密码
String database = "JXC";//数据库名字
String url = "D:/jxc.sql";//备份的目的地址
boolean check = com.backupDatebase(ip,username,password,database,url);
if(check){
%>
数据库备份成功
<%} %>
</body>
</html>
下面是恢复数据的代码。
Java代码代码
package cn.qm.db;
import java.io.*;
import java.lang.*;
/*
* 还原MySql数据库
* */
public class Recover {
public boolean load(){
String filepath = "d:\\jxc.sql"; // 备份的路径地址
//新建数据库test
String stmt1 = "mysqladmin -u root -proot create jxctest";
String stmt2 = "mysql -u root -proot jxctest < " + filepath;
String[] cmd = { "cmd", "/c", stmt2 };
try {
Runtime.getRuntime().exec(stmt1);
Runtime.getRuntime().exec(cmd);
System.out.println("数据已从 " + filepath + " 导入到数据库中");
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
}
Html代码代码
<%@ page language="java" import="java.util.*,cn.qm.db.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>数据恢复测试</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
Recover com = new Recover();
String url = "D:/jxc.sql";
boolean check = com.load();
if(check){
%>
数据库恢复成功
<%} %>
</body>
</html>
相关文章推荐
- JSP 实现 之 调用java方法实现MySQL数据库备份和恢复
- JS+JSP通过img标签调用实现静态页面访问次数统计的方法
- Java 下实现对mysql数据库的备份和恢复
- java实现mySQL数据库的备份与恢复
- 通过java备份恢复mysql数据库的实现代码
- javabean操作文件正确,但是Jsp调用javabean时文件路径出错问题解决之JavaBean访问本地文件实现路径无关实现方法
- 如何用Java实现MySQL数据库的备份和恢复
- SQL学习:mysql数据库备份和使用日志恢复+python调用mysql方法(4)
- 如何用Java实现MySQL数据库的备份和恢复
- JS+JSP通过img标签调用实现静态页面访问次数统计的方法
- (转)javabean操作文件正确,但是Jsp调用javabean时文件路径出错问题解决之JavaBean访问本地文件实现路径无关实现方法
- 如何用Java实现MySQL数据库的备份和恢复
- jsp页面:js方法里嵌套java代码(是操作数据库的),如果这个js 方法没被调用,当jsp页面被解析的时候,不管这个js方法有没有被调用这段java代码都会被执行?
- 用window.open方法实现子页面调用的父子页面传值
- jsp实现将动态网页转换成静态页面的方法
- jQuery AJAX实现调用页面后台方法。调用ashx方法
- 如何用SQLDMO在ASP.NET页面下实现数据库的备份与恢复
- java反射中两种方法的不同(反射能否实现对类的私有方法的调用)
- dedecms实现任意页面调用当前会员信息的方法
- java中由类名和方法名字符串实现其调用