81,092
社区成员
发帖
与我相关
我的任务
分享
String webtruepath = "C:/web/sqlbeifen/2017-01-11/1484113515125.bak";
String name = "1484113515125.bak";
String dbname = "hoosee_ydoa";
File file = new File(webtruepath);
// String path = file.getPath() + "\\" + name + ".bak";// name文件名
String path="C:/web/sqlbeifen/2017-01-11/1484113515125.bak";
String recoverySql = "ALTER DATABASE hoosee_ydoa SET ONLINE WITH ROLLBACK IMMEDIATE";// 恢复所有连接
String guocheng="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 ";
PreparedStatement ps = DataBaseUtil.getConnection().prepareStatement(recoverySql);
CallableStatement cs = DataBaseUtil.getConnection().prepareCall(guocheng);
cs.setString(1, dbname); // 数据库名
cs.setString(2, path); // 已备份数据库所在路径
cs.execute(); // 还原数据库
ps.execute(); // 恢复数据库连接
return;
/**
* 获取数据库连接
* @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=hoosee_ydoa";
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();
}
}
}
String webtruepath = "C:/web/sqlbeifen/2017-01-11/1484113515125.bak";
String name = "1484113515125.bak";
String dbname = "hoosee_ydoa";
File file = new File(webtruepath);
// String path = file.getPath() + "\\" + name + ".bak";// name文件名
String path="C:/web/sqlbeifen/2017-01-11/1484113515125.bak";
String recoverySql = "ALTER DATABASE hoosee_ydoa SET ONLINE WITH ROLLBACK IMMEDIATE";// 恢复所有连接
long a=new Date().getTime();//随机数
String guocheng="create proc killrestore"+a+" (@dbname varchar(20),@dbpath varchar(40)) "+
"as "+
"begin "+
" declare @sql nvarchar(500) "+
" declare @spid int "+
" set @sql='declare getspid cursor for select spid from sys.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 ";
//dao.getSingle(guocheng);
jdbc.update(guocheng);//执行这个存储过程
PreparedStatement ps = DataBaseUtil.getConnection().prepareStatement(recoverySql);
CallableStatement cs = DataBaseUtil.getConnection().prepareCall("{call killrestore"+a+" (?,?)}");
cs.setString(1, dbname); // 数据库名
cs.setString(2, path); // 已备份数据库所在路径
cs.execute(); // 还原数据库
ps.execute(); // 恢复数据库连接
jdbc.update(guocheng);//执行这个存储过程
PreparedStatement ps = DataBaseUtil.getConnection().prepareStatement(recoverySql);
这里执行了那个连接,就是在
cs.execute(); // 还原数据库
ps.execute(); // 恢复数据库连接
这里出现连接错误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
这段你要现在数据库里执行 [/quote]
要数据库里面执行的话 那这样就不能单用web端来 操作了。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(); // 恢复数据库连接
} catch (Exception e) {
e.printStackTrace();
}
return "recovery";
}
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
这段你要现在数据库里执行 CallableStatement cs = DataBaseUtil.getConnection().prepareCall(guocheng);
cs.setString(1, dbname); // 数据库名
cs.setString(2, path);
其面要有与之对应的? 你前面那段SQL 里貌似没有啊