1.连接数据库的代码:
package com.once.xfd.dbutil; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class DataBaseUtil { /** * 获取数据库连接 * @return Connection 对象 */ public static Connection 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(); } return conn; } public static void closeConn(Connection conn) { if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
2.备份功能代码:
/** * 备份数据库 * @return backup * @throws Exception */ public String backup() { ActionContext context = ActionContext.getContext(); HttpServletRequest request = (HttpServletRequest) context .get(ServletActionContext.HTTP_REQUEST); String webtruepath = request.getParameter("path"); String name = "dbname"; //数据库名 try { File file = new File(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.还原数据库代码(调用存储过程)
/** * 数据库还原 * @return recovery */ public String 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 = new File(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) open getspid fetch next from getspid into @spid while @@fetch_status <> -1 begin exec('kill '+@spid) fetch next from getspid into @spid end close getspid deallocate getspid restore database @dbname from disk= @dbpath with replace end
注意:存储过程创建在系统数据库master下面,切记,否则会报错!!!
在执行数据还原之前应该在master数据库中建立此存储过程,否则会报错。
建立存储过程的步骤如下:
打开SqlServer-->数据库-->系统数据库-->master-->可编程性-->存储过程-->右击存储过程-->在新打开的页面中复制上面的存储过程的代码-->全选这段代码-->执行这段代码-->刷新master数据库,就会出现新建的存储过程了
如下图所示: