Java实现Sqlserver及MySql的备份与还原
注:本人是采用Struts1做的一个简单小例子。
实现步骤:
1.数据库基类
package com.wingo.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* @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 {
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);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
/**
* @Description: 关闭
* @author 莫希柏
* @date Jul 4, 2012 2:22:57 PM
*/
public static void closeConn(Connection conn) {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
2.action类
package com.wingo.action;
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;
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;
import com.wingo.util.DataBaseUtil;
public class DataAction extends DispatchAction{
/**
* @Description: SqlServer备份
* mysql下载哪个版本好2022@author 莫希柏
* @date Jul 4, 2012 2:45:16 PM
*/
public ActionForward doSqlServerBackUp(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String mssqlBackupName=
Parameter("mssqlBackupName");//自定义备份数据库名
String mssqlBackupPath=
Parameter("mssqlBackupPath");//自定义备份数据库保存路径
String dbName="datatest";//被备份的数据库名称
boolean flag=false;
try {
File file = new File(mssqlBackupPath);
String path = Path() + "\\"
+ mssqlBackupName + ".bak";//备份生成的数据路径及文件名
String bakSql = "backup database "
+dbName+" to disk=? with init";//备份数据库SQL语句
PreparedStatement bak = DataBaseUtil.getConnection()
.prepareStatement(bakSql);
bak.setString(1, path);//path必须是绝对路径
ute(); //备份数据库
bak.close();
flag=true;
} catch (Exception e) {
flag=false;
e.printStackTrace();
}
response.setCharacterEncoding("utf-8");
try {
if(flag==true){
Writer().print(
"<script type=\"text/javascript\">
alert('SQLSERVER备份成功!');document.location.href='DataAction.do?action=toBackIndex';</script>");
}else{
Writer().print(
"<script type=\"text/javascript\">
alert('SQLSERVER备份失败!');document.location.href='DataAction.do?action=toBackIndex';</script>");
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* @Description: SqlServer还原
* @author 莫希柏
* @date Jul 4, 2012 4:28:05 PM
*/
public ActionForward doSqlServerRecovery(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response){
boolean flag = false;
String mssqlRecoveryPath =
Parameter("mssqlRecoveryPath"); //被还原数据库文件的路径
String dbname="datatest";//数据库名称
try{
File file = new File(mssqlRecoveryPath);
String path = Path();//数据库文件名
String recoverySql = "ALTER DATABASE "
+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(); // 恢复数据库连接
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论