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)
<%@ 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>
