当前位置:
文档之家› JAVA实现SQLSERVER及MYSQL的备份与还原(经典版)
JAVA实现SQLSERVER及MYSQL的备份与还原(经典版)
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 doSqlServerBackUp(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception { String mssqlBackupName= request.getParameter("mssqlBackupName");//自定义备份数据库名 String mssqlBackupPath=
request.getParameter("mssqlRecoveryPath"); // 被还原数据库文件的路径 String dbname="datatest";//数据库名称 try{ File file = new File(mssqlRecoveryPath); String path = file.getPath();//数据库文件名 String recoverySql = "ALTER DATABASE "
* @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 {
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;
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 "
} catch (ClassNotFoundException e) { e.printStackTrace();
} catch (SQLException e) { e.printStackTrace();
} return conn; }
/** * @Description: 关闭 * @author 莫希柏 * @date Jul 4, 2012 2:22:57 PM
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 */
public ActionForward doMySqlBackUp (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){
boolean flag=false; try {
+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\">
Java 实现 Sqlserver 及 MySql 的备份与还原
注:本人是采用 Struts1 做的一个简单小例子。 实现步骤: 1.数据库基类 package com.wingo.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; /**
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 */
+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){
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\">
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);