关于数据库进行还原的问题

zhiyuku4 2017-01-12 10:15:10
这是还原操作的代码

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();
}
}
}


现在我执行还原以后,就出现报错。出现
com.microsoft.sqlserver.jdbc.SQLServerException: 索引 1 超出范围。 com.microsoft.sqlserver.jdbc.SQLServerException: 索引 1 超出范围。
百度以后发现这个索引超出范围是因为??的问
不知道大佬有其他方法或者能帮看看这是什么情况,
...全文
570 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
bcsflilong 2017-01-12
  • 打赏
  • 举报
回复
在还原的时候数据库的确是停掉的
zhiyuku4 2017-01-12
  • 打赏
  • 举报
回复
引用 14 楼 bcsflilong 的回复:
ps.execute(); // 恢复数据库连接 这个ps是PreparedStatement ps = DataBaseUtil.getConnection().prepareStatement(recoverySql); 你试试在这里修改一些 在重新获取Connection 不用DataBaseUtil的方法
不对劲,他是在还原的时候直接错误了, cs.execute(); // 还原数据库 和重新连接没有关系吧
bcsflilong 2017-01-12
  • 打赏
  • 举报
回复
ps.execute(); // 恢复数据库连接 这个ps是PreparedStatement ps = DataBaseUtil.getConnection().prepareStatement(recoverySql); 你试试在这里修改一些 在重新获取Connection 不用DataBaseUtil的方法
zhiyuku4 2017-01-12
  • 打赏
  • 举报
回复
引用 12 楼 bcsflilong 的回复:
ALTER DATABASE hoosee_ydoa SET ONLINE WITH ROLLBACK IMMEDIATE 执行创建过程之后 把这句执行一下

        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(); // 恢复数据库连接 这里出现连接错误
bcsflilong 2017-01-12
  • 打赏
  • 举报
回复
ALTER DATABASE hoosee_ydoa SET ONLINE WITH ROLLBACK IMMEDIATE 执行创建过程之后 把这句执行一下
zhiyuku4 2017-01-12
  • 打赏
  • 举报
回复
引用 10 楼 bcsflilong 的回复:
[quote=引用 9 楼 zhiyuku4 的回复:] [quote=引用 7 楼 bcsflilong 的回复:] [quote=引用 6 楼 zhiyuku4 的回复:] [quote=引用 5 楼 bcsflilong 的回复:]
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";  
    }  
按照这样写的话 他是报错 com.microsoft.sqlserver.jdbc.SQLServerException: 找不到存储过程 'killrestore'。 com.microsoft.sqlserver.jdbc.SQLServerException: 找不到存储过程 'killrestore'。[/quote] 报这个错误 是因为 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] 懂了 先执行这个存储过程 然后在恢复[/quote]对的 数据库里要现有这个过程 你才能调用[/quote] 现在又有新的问题 ,执行过程以后就连接不了 严重: JDBC begin failed com.microsoft.sqlserver.jdbc.SQLServerException: Connection reset by peer: socket write error
bcsflilong 2017-01-12
  • 打赏
  • 举报
回复
引用 9 楼 zhiyuku4 的回复:
[quote=引用 7 楼 bcsflilong 的回复:] [quote=引用 6 楼 zhiyuku4 的回复:] [quote=引用 5 楼 bcsflilong 的回复:]
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";  
    }  
按照这样写的话 他是报错 com.microsoft.sqlserver.jdbc.SQLServerException: 找不到存储过程 'killrestore'。 com.microsoft.sqlserver.jdbc.SQLServerException: 找不到存储过程 'killrestore'。[/quote] 报这个错误 是因为 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] 懂了 先执行这个存储过程 然后在恢复[/quote]对的 数据库里要现有这个过程 你才能调用
zhiyuku4 2017-01-12
  • 打赏
  • 举报
回复
引用 7 楼 bcsflilong 的回复:
[quote=引用 6 楼 zhiyuku4 的回复:] [quote=引用 5 楼 bcsflilong 的回复:]
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";  
    }  
按照这样写的话 他是报错 com.microsoft.sqlserver.jdbc.SQLServerException: 找不到存储过程 'killrestore'。 com.microsoft.sqlserver.jdbc.SQLServerException: 找不到存储过程 'killrestore'。[/quote] 报这个错误 是因为 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] 懂了 先执行这个存储过程 然后在恢复
zhiyuku4 2017-01-12
  • 打赏
  • 举报
回复
引用 4 楼 bcsflilong 的回复:
[quote=引用 3 楼 zhiyuku4 的回复:] [quote=引用 1 楼 bcsflilong 的回复:] 你正常调用
CallableStatement cs = DataBaseUtil.getConnection().prepareCall(guocheng);  
            cs.setString(1, dbname); // 数据库名  
            cs.setString(2, path); 
其面要有与之对应的? 你前面那段SQL 里貌似没有啊
http://blog.csdn.net/tkd03072010/article/details/6668940 我是参考这个博主的代码,里面的存储过程 就是要他的[/quote]
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端来 操作了。
bcsflilong 2017-01-12
  • 打赏
  • 举报
回复
引用 6 楼 zhiyuku4 的回复:
[quote=引用 5 楼 bcsflilong 的回复:]
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";  
    }  
按照这样写的话 他是报错 com.microsoft.sqlserver.jdbc.SQLServerException: 找不到存储过程 'killrestore'。 com.microsoft.sqlserver.jdbc.SQLServerException: 找不到存储过程 'killrestore'。[/quote] 报这个错误 是因为 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 这个你没在数据里先执行创建这个存储过程
zhiyuku4 2017-01-12
  • 打赏
  • 举报
回复
引用 5 楼 bcsflilong 的回复:
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";  
    }  
按照这样写的话 他是报错 com.microsoft.sqlserver.jdbc.SQLServerException: 找不到存储过程 'killrestore'。 com.microsoft.sqlserver.jdbc.SQLServerException: 找不到存储过程 'killrestore'。
bcsflilong 2017-01-12
  • 打赏
  • 举报
回复
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";  
    }  
bcsflilong 2017-01-12
  • 打赏
  • 举报
回复
引用 3 楼 zhiyuku4 的回复:
[quote=引用 1 楼 bcsflilong 的回复:] 你正常调用
CallableStatement cs = DataBaseUtil.getConnection().prepareCall(guocheng);  
            cs.setString(1, dbname); // 数据库名  
            cs.setString(2, path); 
其面要有与之对应的? 你前面那段SQL 里貌似没有啊
http://blog.csdn.net/tkd03072010/article/details/6668940 我是参考这个博主的代码,里面的存储过程 就是要他的[/quote]
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     
这段你要现在数据库里执行
zhiyuku4 2017-01-12
  • 打赏
  • 举报
回复
引用 1 楼 bcsflilong 的回复:
你正常调用
CallableStatement cs = DataBaseUtil.getConnection().prepareCall(guocheng);  
            cs.setString(1, dbname); // 数据库名  
            cs.setString(2, path); 
其面要有与之对应的? 你前面那段SQL 里貌似没有啊
http://blog.csdn.net/tkd03072010/article/details/6668940 我是参考这个博主的代码,里面的存储过程 就是要他的
bcsflilong 2017-01-12
  • 打赏
  • 举报
回复
http://blog.sina.com.cn/s/blog_5f1fe33f0100dkn0.html
bcsflilong 2017-01-12
  • 打赏
  • 举报
回复
你正常调用
CallableStatement cs = DataBaseUtil.getConnection().prepareCall(guocheng);  
            cs.setString(1, dbname); // 数据库名  
            cs.setString(2, path); 
其面要有与之对应的? 你前面那段SQL 里貌似没有啊

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧