JAvaプログラムエクスポートデータベーススクリプト


Listenerをカスタマイズし、毎日特定の時間にデータベースをバックアップします.
public class AutoBackUpListener implements ServletContextListener{
	
	private Timer timer;
	
	public void contextDestroyed(ServletContextEvent arg0) {
		 System.out.println("shut down task.");
		 timer.cancel(); //Terminate the timer thread
	}

	public void contextInitialized(ServletContextEvent arg0) {
		timer = new Timer(); 
		System.out.println("begin task.");
		 
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, 03);//   3:30 am      
		calendar.set(Calendar.MINUTE, 30);
		calendar.set(Calendar.SECOND, 0);
		Date time = calendar.getTime();
		timer = new Timer();
		//timer.schedule(new RemindTask(), time);
        timer.scheduleAtFixedRate(new Beifen(), time, 1000*60*60*24);//      
	   
	}

	class Beifen extends TimerTask {
		private String user_name;//         
	    private String user_psw;//        
	    private String db_name;//             
	    private String host_ip;   
	    private String user_charset;   
	    private String backup_path; //            
	    private String stmt;   
	    public Beifen(){}
	    
	    public Beifen(String user_name,String user_psw,String db_name,String host_ip,String user_charset,String backup_path){   
	        this.user_name=user_name;   
	        this.user_psw=user_psw;   
	        this.db_name=db_name;   
	        //  IP;   
	        if(host_ip==null||host_ip.equals(""))   
	            this.host_ip="localhost";//        
	        else  
	            this.host_ip=host_ip;   
	        //      
	        if(user_charset==null||user_charset.equals(""))   
	            this.user_charset=" "; //               
	        else  
	            this.user_charset=" --default-character-set="+user_charset;   
	        this.backup_path=backup_path;   
	  
	        /*this.stmt="C:\\Program Files\\MySQL\\MySQL Server 5.0\\bin\\mysqldump "+this.db_name+" -h "+this.host_ip+" -u"+this.user_name+" -p"+this.user_psw   
	                +this.user_charset+" --result-file="+this.backup_path; */  
	         this.stmt="D:\\AppServ\\MySQL\\bin\\mysqldump "+this.db_name+" -h "+this.host_ip+" -u"+this.user_name+" -p"+this.user_psw   
            +this.user_charset+" --result-file="+this.backup_path;
	    } 
	   
	    public boolean backup_run(){   
	        boolean run_result=false;   
	        try{   
	            Runtime.getRuntime().exec(this.stmt);   
	            run_result=true;   
	        }catch(Exception e){   
	            e.printStackTrace();   
	        }finally{   
	            return run_result;   
	        }   
	    } 
	    
		@Override
		public void run() {
			Beifen backup=new Beifen("root","root","tansuo_mobile",null,"utf8","d:\\tansuo_moblie.sql");   
	        boolean result=backup.backup_run();   
	        if(result)   
	            System.out.println("    ");
		}
		
	}
	

}

 
jspストリームを使用してサーバからsqlファイルをダウンロードします.
 
<%@ page language="java" pageEncoding="utf-8"%>
<%@ page import="java.io.OutputStream,java.io.File,java.io.FileInputStream"%>
<%@page import="java.io.PrintWriter"%>
<html>
  <head>
  
    <title>JSP     </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">

  </head>
  
  <body>
   <%      
          response.reset();
          OutputStream o =response.getOutputStream();     
          byte   b[]=new   byte[500];  
          String path = "d:\\tansuo_moblie.sql"; //        
          String myName = "     .sql";        //       
          //String path = request.getSession().getServletContext().getRealPath("/");
         // if(path.endsWith("\\"))
          //{
           //   path+="upload\\excelfiles\\TransInfo.xls";
          //}
          //else
          //{
            //  path+="upload\\excelfiles\\TransInfo.xls";
          //}
          File fileLoad=new File(path);  
          response.reset();  
          response.setCharacterEncoding("GBK"); 
          request.setCharacterEncoding("GBK"); 
          
          response.setContentType("application/octet-stream; charset=GBK");     
		  response.addHeader("Content-Disposition", "attachment; filename=\"" + new String(myName.getBytes("gb2312"),"iso8859-1") + "\"");  
        
          long   fileLength=fileLoad.length();   
          String   length=String.valueOf(fileLength);   
          response.setHeader("Content_Length",length);   
          FileInputStream   in=new   FileInputStream(fileLoad);   
          int   n=0;   
          while((n=in.read(b))!=-1){ 
             o.write(b,0,n);
          }  
		   
          in.close();
          o.close();
         
  %>
  </body>
</html>