在JSP页面调用JAVA方法实现MySQL数据库的备份和恢复

来源:互联网 发布:淘宝卖家退货率 编辑:程序博客网 时间:2024/06/05 22:55

今天弄了好久,终于搞定了这个问题。使用java代码实现数据库的备份。

Java代码代码  收藏代码
  1. package cn.qm.db;  
  2. import java.io.BufferedReader;  
  3. import java.io.DataInputStream;  
  4. import java.io.IOException;  
  5. import java.io.InputStreamReader;  
  6.   
  7.   
  8. public class Command {  
  9.     /*  
  10.     public static void main(String[] args) throws IOException {  
  11.         Command com = new Command();  
  12.         com.backupDatebase("localhost","root","root""JXC""D:/jxc.sql");  
  13.     }  
  14.       
  15.     /**  
  16.      * 执行dos命令  
  17.      * @param cmd  
  18.      * @return  
  19.      */  
  20.     public String execCmd(String cmd) {  
  21.         StringBuffer sb = new StringBuffer("");  
  22.         StringBuffer str = new StringBuffer();  
  23.         str.append("cmd.exe /c \"").append(cmd).append("\"");  
  24.         System.out.println(str);        //打印执行的命令  
  25.         Process ls_proc;  
  26.         try {  
  27.             ls_proc = Runtime.getRuntime().exec(str.toString());  
  28.             BufferedReader in = new BufferedReader(  
  29.                                     new InputStreamReader(  
  30.                                         new DataInputStream(ls_proc.getInputStream())));  
  31.             String ss = "";  
  32.             while((ss = in.readLine()) != null) {  
  33.                 sb.append(ss).append("\n");  
  34.             }  
  35.             in.close();  
  36.         } catch (IOException e) {  
  37.             e.printStackTrace();  
  38.         }   
  39.   
  40.         return sb.toString();  
  41.     }  
  42.       
  43.     /**  
  44.      * 执行mysql数据库备份  
  45.      * @param ip  
  46.      * @param username  
  47.      * @param password  
  48.      * @param datebaseName  
  49.      * @param filePath  
  50.      * @return  
  51.      */  
  52.     public boolean backupDatebase(String ip, String username, String password,String datebaseName, String filePath) {  
  53.         String strCommand = "mysqldump -h "+ip+" -u" + username + " -p" + password + " " + datebaseName + " > " + filePath;  
  54.         String result = execCmd(strCommand);  
  55.         System.out.println(result);  
  56.         return true;  
  57.     }  
  58.       
  59.     /**  
  60.      * 根据返回结果验证是否成功  
  61.      * @param result  
  62.      * @return  
  63.      */  
  64.     public boolean check(String result) {  
  65.         return true;  
  66.     }  
  67. }  

 在JSP页面只要调用这个JAVA类就可以。(文件名只能是.sql)

Html代码代码  收藏代码
  1. <%@ page language="java" import="java.util.*,cn.qm.db.*" pageEncoding="UTF-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>数据库备份测试</title>  
  13.       
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.     <meta http-equiv="description" content="This is my page">  
  19.     <!--  
  20.     <link rel="stylesheet" type="text/css" href="styles.css">  
  21.     -->  
  22.   
  23.   </head>  
  24.     
  25.   <body>  
  26. <%  
  27. Command com = new Command();  
  28. String ip = "localhost";//ip地址  
  29. String username = "root";//MySQL数据库的用户名  
  30. String password = "root";//MySQL数据库的密码  
  31. String database = "JXC";//数据库名字  
  32. String url = "D:/jxc.sql";//备份的目的地址  
  33. boolean check = com.backupDatebase(ip,username,password,database,url);  
  34. if(check){  
  35.  %>  
  36.  数据库备份成功  
  37.  <%} %>  
  38.   </body>  
  39. </html>  

 下面是恢复数据的代码。

Java代码代码  收藏代码
  1. package cn.qm.db;  
  2. import java.io.*;   
  3. import java.lang.*;   
  4.   
  5. /*   
  6. * 还原MySql数据库   
  7. * */   
  8. public class Recover {   
  9. public boolean load(){  
  10.     String filepath = "d:\\jxc.sql"; // 备份的路径地址    
  11.       //新建数据库test   
  12.   
  13.       String stmt1 = "mysqladmin -u root -proot create jxctest";   
  14.   
  15.       String stmt2 = "mysql -u root -proot jxctest < " + filepath;   
  16.       String[] cmd = { "cmd""/c", stmt2 };   
  17.   
  18.       try {   
  19.       Runtime.getRuntime().exec(stmt1);   
  20.       Runtime.getRuntime().exec(cmd);   
  21.       System.out.println("数据已从 " + filepath + " 导入到数据库中");   
  22.       } catch (IOException e) {   
  23.       e.printStackTrace();   
  24.       }   
  25.       return true;  
  26. }  
  27. }   

 

Html代码代码  收藏代码
  1. <%@ page language="java" import="java.util.*,cn.qm.db.*" pageEncoding="UTF-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>数据恢复测试</title>  
  13.       
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.     <meta http-equiv="description" content="This is my page">  
  19.     <!--  
  20.     <link rel="stylesheet" type="text/css" href="styles.css">  
  21.     -->  
  22.   
  23.   </head>  
  24.     
  25.   <body>  
  26.   
  27. <%  
  28. Recover com = new Recover();  
  29. String url = "D:/jxc.sql";  
  30. boolean check = com.load();  
  31. if(check){  
  32.  %>  
  33.  数据库恢复成功  
  34.  <%} %>  
  35.   </body>  
  36. </html>