Java实现SqlServer及MySql的备份与还原(经典版)

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

Java实现Sqlserver及MySql的备份与还原注:本人是采用Struts1做的一个简单小例子。实现步骤:1.数据库基类packagecom.wingo.util;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.SQLException;/***@ClassName:DataBaseUtil*@Description:TODO*@author莫希柏*@dateJul4,20122:21:41PM*/publicclassDataBaseUtil{/***@Description:获取数据库连接*@author莫希柏*@dateJul4,20122:23:11PM*/publicstaticConnectiongetConnection(){Connectionconn=null;try{Class.forName(com.microsoft.jdbc.sqlserver.SQLServerDriver);Stringurl=jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=datatest;Stringusername=sa;Stringpassword=sa;conn=DriverManager.getConnection(url,username,password);}catch(ClassNotFoundExceptione){e.printStackTrace();}catch(SQLExceptione){e.printStackTrace();}returnconn;}/***@Description:关闭*@author莫希柏*@dateJul4,20122:22:57PM*/publicstaticvoidcloseConn(Connectionconn){if(conn!=null){try{conn.close();}catch(SQLExceptione){e.printStackTrace();}}}}2.action类packagecom.wingo.action;importjava.io.BufferedReader;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.io.OutputStream;importjava.io.OutputStreamWriter;importjava.sql.CallableStatement;importjava.sql.PreparedStatement;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importorg.apache.struts.action.ActionForm;importorg.apache.struts.action.ActionForward;importorg.apache.struts.action.ActionMapping;importorg.apache.struts.actions.DispatchAction;importcom.wingo.util.DataBaseUtil;publicclassDataActionextendsDispatchAction{/***@Description:SqlServer备份*@author莫希柏*@dateJul4,20122:45:16PM*/publicActionForwarddoSqlServerBackUp(ActionMappingmapping,ActionFormform,HttpServletRequestrequest,HttpServletResponseresponse)throwsException{StringmssqlBackupName=request.getParameter(mssqlBackupName);//自定义备份数据库名StringmssqlBackupPath=request.getParameter(mssqlBackupPath);//自定义备份数据库保存路径StringdbName=datatest;//被备份的数据库名称booleanflag=false;try{Filefile=newFile(mssqlBackupPath);Stringpath=file.getPath()+\\+mssqlBackupName+.bak;//备份生成的数据路径及文件名StringbakSql=backupdatabase+dbName+todisk=?withinit;//备份数据库SQL语句PreparedStatementbak=DataBaseUtil.getConnection().prepareStatement(bakSql);bak.setString(1,path);//path必须是绝对路径bak.execute();//备份数据库bak.close();flag=true;}catch(Exceptione){flag=false;e.printStackTrace();}response.setCharacterEncoding(utf-8);try{if(flag==true){response.getWriter().print(scripttype=\text/javascript\alert('SQLSERVER备份成功!');document.location.href='DataAction.do?action=toBackIndex';/script);}else{response.getWriter().print(scripttype=\text/javascript\alert('SQLSERVER备份失败!');document.location.href='DataAction.do?action=toBackIndex';/script);}}catch(IOExceptione){e.printStackTrace();}returnnull;}/***@Description:SqlServer还原*@author莫希柏*@dateJul4,20124:28:05PM*/publicActionForwarddoSqlServerRecovery(ActionMappingmapping,ActionFormform,HttpServletRequestrequest,HttpServletResponseresponse){booleanflag=false;StringmssqlRecoveryPath=request.getParameter(mssqlRecoveryPath);//被还原数据库文件的路径Stringdbname=datatest;//数据库名称try{Filefile=newFile(mssqlRecoveryPath);Stringpath=file.getPath();//数据库文件名StringrecoverySql=ALTERDATABASE+dbname+SETONLINEWITHROLLBACKIMMEDIATE;//断开所有连接PreparedStatementps=DataBaseUtil.getConnection().prepareStatement(recoverySql);CallableStatementcs=DataBaseUtil.getConnection().prepareCall({callkillrestore(?,?)});//调用存储过程cs.setString(1,dbname);//数据库名cs.setString(2,path);//已备份数据库所在路径cs.execute();//还原数据库ps.execute();//恢复数据库连接flag=true;}catch(Exceptione){flag=false;e.printStackTrace();}response.setCharacterEncoding(utf-8);try{if(flag==true){response.getWriter().print(scripttype=\text/javascript\alert('SQLSERVER备份成功!');document.location.href='DataAction.do?action=toBackIndex';/script);}else{response.getWriter().print(scripttype=\text/javascript\alert('SQLSERVER备份失败!');document.location.href='DataAction.do?action=toBackIndex';/script);}}catch(IOExceptione){e.printStackTrace();}returnnull;}/***@Description:MySql备份*@author莫希柏*@dateJul4,20124:39:02PM*/publicActionForwarddoMySqlBackUp(ActionMappingmapping,ActionFormform,HttpServletRequestrequest,HttpServletResponseresponse){booleanflag=false;try{Runtimert=Runtime.getRuntime();StringmySqlBackupName=request.getParameter(mySqlBackupName);//mysql自定义数据库备份名称StringmysqlBackupPath=request.getParameter(mysqlBackupPath);//mysql自定义数据库备份保存路径StringfPath=mysqlBackupPath+mySqlBackupName+.sql;Stringcommand=C:/ProgramFiles/MySQL/MySQLServer5.0/bin/mysqldump-uroot-p123456datatest;//调用mysql的cmd:Processchild=rt.exec(command);//设置导出编码为utf8。这里必须是utf8//把进程执行中的控制台输出信息写入.sql文件,即生成了备份文件。注:如果不对控制台信息进行读出,则会导致进程堵塞无法运行InputStreamin=child.getInputStream();//控制台的输出信息作为输入流InputStreamReaderinput=newInputStreamReader(in,utf8);//设置输出流编码为utf8。这里必须是utf8,否则从流中读入的是乱码StringinStr;StringBuffersb=newStringBuffer();StringoutStr;//组合控制台输出信息字符串BufferedReaderbr=newBufferedReader(input);while((inStr=br.readLine())!=null){sb.append(inStr+\r\n);}outStr=s

1 / 12
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功