JSP数据库备份和还原

JSP数据库备份和还原

JSP数据库备份
废话不多说Java代码如下:

package com.p.filter;
import java.io.BufferedReader;  
import java.io.DataInputStream;  
import java.io.IOException;  
import java.io.InputStreamReader;  
/**
 * 数据库备份
 * @author Forever_mx
 */
public class Command {
	public static void main(String[] args) {
		   Command com = new Command();  
com.backupDatebase("localhost","root","root","test","D:/test.sql");//输出到控制台  
	}
	//执行dos命令 
	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数据库备份
	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;
	}
	//根据返回结果验证是否成功	
	public boolean check(String result){
		return true;
	}
}

JSP备份代码如下:

<%@ page language="java" import="java.util.*,com.p.filter.*" 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">
  </head>
  <body>
<% 
Command com = new Command();
    String ip = "localhost";//ip地址
    String username = "root";//MySQL数据库用户名
    String password = "root";//MySQL数据库密码
    String datebaseName = "test";//数据库名称
    String filePath = "D:/test.sql";//备份的目的地址
    boolean check = com.backupDatebase(ip, username, password, datebaseName, filePath);
if(check){ 
%>
     "数据库备份成功!!!";
     <%} %>
  </body>
</html>

备份效果图如下:
在这里插入图片描述
注意:有时候备份数据库时,它能备份但是没有数据只显示0KB,这时你需要检查路径,MySQL用户密码是否正确,然后检查dos的执行命名,最后检查MySQL的配置环境变量。一般都在C盘的Program Files下,配置如下:在环境变量Path下新建C:\Program Files\MySQL\MySQL Server 5.7\bin即可。有时候Program Files中间的空格会影响,删除空格即可,一般不会有影响。

JSP数据还原

package com.p.filter;
import java.io.*;
/**
 * 数据库恢复/还原数据库
 * @author Forever_mx
 */
public class Recover {
public boolean load(){
	String filepath = "d:\\test.sql";//备份的路径
	//新建数据库
	String stmt1 = "mysqladmin -u root -proot create newtest"; 
	String stmt2 = "mysql -u root -proot newtest < " + 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;
}
}

JSP还原代码如下:

<%@ page language="java" import="java.util.*,com.p.filter.*" 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">
  </head>
  <body>
    <%
    Recover com = new Recover();
    String url = "D:/test.sql";
    boolean check=com.load();
    if(check){
     %>
      "数据库还原成功!!!";
     <%} %>
  </body>
</html>

恢复数据控制台输出图如下:
在这里插入图片描述

注意:还原数据库是从备份的目的路径还原到MySQL数据库中,如果数据库中有该名称的数据库,它将会覆盖该数据库。

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页