JSP页面备份数据库
今天给大家介绍的是在JSP页面备份MySql数据库,参考这篇文章做的例子,下面将代码分享给大家!
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不到一年,有不足希望大家多多指教!