时间:2021-07-01 10:21:17 帮助过:2人阅读
在JSP页面只要调用这个JAVA类就可以。(文件名只能是.sql)
<%@ 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>
下面是恢复数据的代码。
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; } }
<%@ 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数据库备份和恢复
标签: