java 备份 sql server_Java(JFinal)实现sqlserver2017的数据库的备份与恢复

1.连接数据库的代码:

packagecom.once.xfd.dbutil;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.SQLException;public classDataBaseUtil {/*** 获取数据库连接

*@returnConnection 对象*/

public staticConnection getConnection() {

Connection conn= null;try{

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

String url= "jdbc:sqlserver://127.0.0.1:1433;databaseName=master";

String username= "sa";

String password= "123456";

conn=DriverManager.getConnection(url, username, password);

}catch(ClassNotFoundException e) {

e.printStackTrace();

}catch(SQLException e) {

e.printStackTrace();

}returnconn;

}public static voidcloseConn(Connection conn) {if (conn != null) {try{

conn.close();

}catch(SQLException e) {

e.printStackTrace();

}

}

}

}

2.备份功能代码:

/*** 备份数据库

*@returnbackup

*@throwsException*/

publicString backup() {

ActionContext context=ActionContext.getContext();

HttpServletRequest request=(HttpServletRequest) context

.get(ServletActionContext.HTTP_REQUEST);

String webtruepath= request.getParameter("path");

String name= "dbname"; //数据库名

try{

File file= newFile(webtruepath);

String path= file.getPath() + "\\" + name + ".bak";//name文件名

String bakSQL = "backup database 数据库名 to disk=? with init";//SQL语句

PreparedStatement bak =DataBaseUtil.getConnection()

.prepareStatement(bakSQL);

bak.setString(1, path);//path必须是绝对路径

bak.execute(); //备份数据库

bak.close();

}catch(Exception e) {

e.printStackTrace();

}return "backup";

}

3.还原数据库代码(调用存储过程)

/*** 数据库还原

*@returnrecovery*/publicString recovery() {

ActionContext context=ActionContext.getContext();

HttpServletRequest request=(HttpServletRequest) context

.get(ServletActionContext.HTTP_REQUEST);

String webtruepath= request.getParameter("path");

String name= "******";

String dbname= "******";try{

File file= newFile(webtruepath);

String path= file.getPath() + "\\" + name + ".bak";//name文件名

String recoverySql = "ALTER   DATABASE   数据库名   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(); //恢复数据库连接//执行查询数据库操作

PreparedStatement pStatement=DataBaseUtil.getConnection().prepareStatement("select * from [t_sjbf]");

System.out.println(pStatement.execute());

//将连接异常在后台进行处理,处理完这一次异常之后,就可以建立正常的数据库连接

dao.find("select * from [t_sjbf]");

}catch(Exception e) {

e.printStackTrace();

}return "recovery";

}

代码中标为紫色部分的代码并不会影响数据的还原,只是在JFinal中数据还原后连接不会在第一时间内连接上,这样前台在刷新页面的时候便会出现警告,为了防止前端出现这样的警告,把数据库的连接操作在后台进行处理(通过dao.find("select * from [t_sjbf]");这条语句就可以建立起与数据库的连接)。

4.存储过程代码:

create proc killrestore (@dbname varchar(20),@dbpath varchar(40))as

begin

declare @sql nvarchar(500)declare @spid int

set @sql='declare getspid cursor for select spid from sysprocesses where dbid=db_id('''+@dbname+''')'

exec (@sql)opengetspidfetch next from getspid into @spid

while @@fetch_status <> -1

begin

exec('kill'+@spid)fetch next from getspid into @spid

end

closegetspiddeallocategetspidrestore database @dbname from disk= @dbpath with replace

end

注意:存储过程创建在系统数据库master下面,切记,否则会报错!!!

在执行数据还原之前应该在master数据库中建立此存储过程,否则会报错。

建立存储过程的步骤如下:

打开SqlServer-->数据库-->系统数据库-->master-->可编程性-->存储过程-->右击存储过程-->在新打开的页面中复制上面的存储过程的代码-->全选这段代码-->执行这段代码-->刷新master数据库,就会出现新建的存储过程了

如下图所示:

99c39789d589955690b76850c9b83652.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值