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

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

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 Jul4,20122:21:41PM

*/

public class DataBaseUtil {

/**

*@Description:获取数据库连接

*@author莫希柏

*@date Jul4,20122:23:11PM

*/

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 Jul4,20122:22:57PM

*/

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备份

*@author莫希柏

*@date Jul4,20122:45:16PM

*/

public ActionForward doSqlServerBackUp(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response) throws Exception {

String mssqlBackupName=

request.getParameter("mssqlBackupName");//自定义备份数据库名String mssqlBackupPath=

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 "

+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(

"");

}else{

response.getWriter().print(

"");

}

} catch (IOException e) {

相关文档
最新文档