JSP页面备份数据库

JSP页面备份数据库

今天给大家介绍的是在JSP页面备份MySql数据库,参考这篇文章做的例子,下面将代码分享给大家!

BackupMysql

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class BackupMysql {
	/** 访问MySQL数据库服务器所在的url */
	private String serverUrl;
	/** 访问MySQL数据库的用户名 */
	private String username;
	/** 访问MySQL数据库的密码 */
	private String password;

	public String getServerUrl() {
		return serverUrl;
	}

	public void setServerUrl(String serverUrl) {
		this.serverUrl = serverUrl;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public BackupMysql(String serverUrl, String username, String password) {
		super();
		this.serverUrl = serverUrl;
		this.username = username;
		this.password = password;
	}

	public String backup(String backupPath, String dbName) throws IOException {

		String backupFile = backupPath
				+ dbName
				+ new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss")
						.format(new Date()) + ".sql";

		String mysql = "mysqldump " + "--host=" + serverUrl + " --user="
				+ username + " --password=" + password + " --opt " + dbName
				+ "> " + backupFile;

		java.lang.Runtime.getRuntime().exec("cmd /c " + mysql);

		System.out.println("备份成功!");

		return backupFile;

	}

	public void restore(String restoreFile, String dbName) throws Exception {

		String mysql = "mysql " + "-h" + serverUrl + " -u" + username + " -p"
				+ password + " " + dbName + " < " + restoreFile;

		System.out.println(mysql);

		java.lang.Runtime.getRuntime().exec("cmd /c " + mysql);

		System.out.println("还原成功!");
	}

}
BackUpVO
public class BackUpVO {
	private String id;
	private String time;
	private String filename;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getTime() {
		return time;
	}
	public void setTime(String time) {
		this.time = time;
	}
	public String getFilename() {
		return filename;
	}
	public void setFilename(String filename) {
		this.filename = filename;
	}
}

B ackUpAction

package org.cs.demo.database;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionSupport;

public class BackUpAction extends ActionSupport implements ServletRequestAware{
	private HttpServletRequest request;
	/**
	 * 备份页面初始化
	 */
	public String backup_init(){
		String path = "e:/backup/database";
		File file = new File(path);
		File [] listfile = file.listFiles();
		List<BackUpVO> list = new ArrayList<BackUpVO>();
		if(listfile !=null){
			int i = 0;
			for(File f : listfile){
				String id = i+"";
				long time = f.lastModified();
				Calendar cal=Calendar.getInstance();   
			    cal.setTimeInMillis(time);
			    String filetime = cal.getTime().toLocaleString();
			    String filename = f.getName();
			    BackUpVO backupvo = new BackUpVO();
			    backupvo.setId(id);
			    backupvo.setTime(filetime);
			    backupvo.setFilename("e:/backup/database/"+filename);
				list.add(backupvo);
				i++;
			}
		}
		request.setAttribute("list", list);
		return "bi";
	}
	
	/**
	 *备份
	 */
	public String backup(){
		String serverUrl = "127.0.0.1";
		String userName = "root";
		String pwd = "root";
		String path = "e:/backup/database/house.sql";
		BackupMysql backup = new BackupMysql(serverUrl, userName, pwd);
		try {
			backup.backup(path, "house");
		} catch (IOException e) {
			System.out.println("备份出错.....");
		}
		return "by";
	}
	
	/**
	 * 恢复
	 * @return
	 */
	public String recove(){
		String serverUrl = "127.0.0.1";
		String userName = "root";
		String pwd = "root";
		String path = "e:/backup/database/house.sql";
		BackupMysql backup = new BackupMysql(serverUrl, userName, pwd);
		String restoreFile = request.getParameter("filename");
		try {
			backup.restore(restoreFile, "house");
		} catch (Exception e) {
			System.out.println("恢复出错.....");
		}
		return "ry";
	}
	
	/**
	 * 删除备份文件
	 */
	public String delete(){
		String restoreFile = request.getParameter("filename");
		File file = new File(restoreFile);
		file.delete();
		return "dy";
	}
	
	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		request = arg0;
	}
	
}
backup.jsp

<form action="/House_Manager/backupaction!backup.action" name="form1" method="post">
	    		<input type="submit" value="备份">
	    	</form>
	  		<table border="1" bordercolor="blue">
	  			<thead>
	  				<tr>
	  					<th>编号</th>
	  					<th>备份时间</th>
	  					<th>文件名</th>
	  					<th>操作</th>
	  				</tr>
	  			</thead>
	  			<tbody>
	  				<s:if test="#request.list!=null">
	        		<s:iterator value="#request.list" id="ba">
	        			<tr>
	    					<td><s:property value="#ba.id"/></td>
	    					<td><s:property value="#ba.time"/></td>
	    					<td><s:property value="#ba.filename"/></td>
	    					<td>
	    						<s:url id="recover" action="backupaction!recove.action">
	             				<s:param name="filename" value="%{#ba.filename}"></s:param>
	          					</s:url>
	          					<s:a href="%{recover}">恢复</s:a>
	          					
	          					<s:url id="delete" action="backupaction!delete.action">
	             				<s:param name="filename" value="%{#ba.filename}"></s:param>
	          					</s:url>
	          					<s:a href="%{delete}">删除</s:a>
	    					</td>
	  						</tr>
	        		</s:iterator>
	       		</s:if>
	  			</tbody>
	  		</table>

web.xml 配置文件

<action name="init_backupaction" class="backupaction" method="backup_init">
			<result name="bi">
				/jsp/database/backup.jsp
			</result>
		</action>
		<action name="backupaction" class="backupaction">
			<result name="by" type="redirectAction">
				init_backupaction
			</result>
			<result name="ry" type="redirectAction">
				init_backupaction
			</result>
			<result name="dy" type="redirectAction">
				init_backupaction
			</result>
		</action>

效果图


本人学习Java不到一年,有不足希望大家多多指教!


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值