JAVA实现SQLSERVER及MYSQL的备份与还原(经典版)

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
request.getParameter("mssqlRecoveryPath"); // 被还原数据库文件的路径 String dbname="datatest";//数据库名称 try{ File file = new File(mssqlRecoveryPath); String path = file.getPath();//数据库文件名 String recoverySql = "ALTER DATABASE "
alert('SQLSERVER 备 份 失 败 ! ');document.location.href='DataAction.d o?action=toBackIndex';</script>"); } } catch (IOException e) {
e.printStackTrace(); } return null; } /** * @Description: SqlServer还原 * @author 莫希柏 * @date Jul 4, 2012 4:28:05 PM */
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.actions.DispatchAction;
+dbname+" SET ONLINE WITH ROLLBACK IMMEDIATE";// 断开所有连接 PreparedStatement ps = DataBaseUtil.getConnection() .prepareStatement(recoverySql); CallableStatement cs = DataBaseUtil.getConnection().prepareCall("{call killrestore(?,?)}"); //调用存储过程 cs.setString(1, dbname); // 数据库名 cs.setString(2, path); // 已备份数据库所在路径 cs.execute(); // 还原数据库 ps.execute(); // 恢复数据库连接 flag=true; } catch (Exception e) { flag=false; e.printStackTrace(); } response.setCharacterEncoding("utf-8"); try { if(flag==true){ response.getWriter().print( "<script type=\"text/javascript\">
Runtime rt = Runtime.getRuntime(); String mySqlBackupName
=request.getParameter("mySqlBackupName");//mysql 自定义数据库备份名称 String mysqlBackupPath
=request.getParameter("mysqlBackupPath");//mysql 自定义数据库备份保存路径 String fPath=mysqlBackupPath+mySqlBackupName+".sql"; String command="C:/Program Files/MySQL/MySQL Server
public ActionForward doMySqlBackUp (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){
boolean flag=false; try {
+dbName+" to disk=? with init";//备份数据库SQL语句 PreparedStatement bak = DataBaseUtil.getConnection()
.prepareStatement(bakSql); bak.setString(1, path);//path必须是绝对路径 bak.execute(); //备份数据库 bak.close(); flag=true; } catch (Exception e) { flag=false; e.printStackTrace(); } response.setCharacterEncoding("utf-8"); try { if(flag==true){
request.getParameter("mssqlBackupPath");//自定义备份数 据库保存路径 String dbName="datatest";//被备份的数据库名称 boolean flag=false; try { File file = new File(mssqlBackupPath); String path = file.getPath() + "\\" + mssqlBackupName + ".bak";//备份生成的数据路径及文件名 String bakSql = "backup database "
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); String url = "jdbc:microsoft:sqlserver://127.0.0.1:1433;
DatabaseName=datatest"; String username = "sa"; String password = "sa"; conn = DriverManager.getConnection(url, username, password);
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.sql.CallableStatement; import java.sql.PreparedStatement;
public ActionForward doSqlServerBackUp(ActLeabharlann BaiduonMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception { String mssqlBackupName= request.getParameter("mssqlBackupName");//自定义备份数据库名 String mssqlBackupPath=
} catch (ClassNotFoundException e) { e.printStackTrace();
} catch (SQLException e) { e.printStackTrace();
} return conn; }
/** * @Description: 关闭 * @author 莫希柏 * @date Jul 4, 2012 2:22:57 PM
* @ClassName: DataBaseUtil * @Description: TODO * @author 莫希柏 * @date Jul 4, 2012 2:21:41 PM */ public class DataBaseUtil { /** * @Description: 获取数据库连接 * @author 莫希柏 * @date Jul 4, 2012 2:23:11 PM */ public static Connection getConnection() { Connection conn = null; try {
*/
public static void closeConn(Connection conn) { if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } }
}
} 2.action 类 package com.wingo.action;
public ActionForward doSqlServerRecovery(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response){
boolean flag = false; String mssqlRecoveryPath =
import com.wingo.util.DataBaseUtil;
public class DataAction extends DispatchAction{ /** * @Description: SqlServer备份 * @author 莫希柏 * @date Jul 4, 2012 2:45:16 PM */
response.getWriter().print( "<script type=\"text/javascript\">
alert('SQLSERVER 备 份 成 功 ! ');document.location.href='DataAction.d o?action=toBackIndex';</script>"); }else{ response.getWriter().print( "<script type=\"text/javascript\">
alert('SQLSERVER 备 份 成 功 ! ');document.location.href='DataAction.d o?action=toBackIndex';</script>"); }else{ response.getWriter().print( "<script type=\"text/javascript\">
alert('SQLSERVER 备 份 失 败 ! ');document.location.href='DataActi on.do?action=toBackIndex';</script> "); } } catch (IOException e) { e.printStackTrace(); } return null; } /** * @Description: MySql备份 * @author 莫希柏 * @date Jul 4, 2012 4:39:02 PM */
Java 实现 Sqlserver 及 MySql 的备份与还原
注:本人是采用 Struts1 做的一个简单小例子。 实现步骤: 1.数据库基类 package com.wingo.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; /**
相关文档
最新文档