数据库备份与恢复java代码_JAVA程序中 实现数据库备份与恢复

做一个项目中的数据库备份与恢复(MSSQL) 数据库备份 String webtruepath=getServletContext().getRealPath(request.getServletPath());//取Servlet的真实路径 java.io.File file=new java.io.File(webtruepath); file=file.getParentFile(); String path=file.getPath()+"//..//WEB-INF//lib//"+name+".dbbak";//name文件名 String bakSQL="backup database school to disk=? with init";//SQL语句 java.sql.PreparedStatement bak=DBS.getConnection().prepareStatement(bakSQL); bak.setString(1,path);//path必须是绝对路径 if(!bak.execute())over="备份成功"; else over="备份失败"; bak.close(); 数据库恢复 if(!DBS.close()){                     over="关闭所有链接失败"; }else{                     String webtruepath=getServletContext().getRealPath(request.getServletPath());                     java.io.File file=new java.io.File(webtruepath);                     file=file.getParentFile();                     String path=file.getPath()+"//..//WEB-INF//lib//"+name;                     String reSQL="restore database school from disk=? with replace";                     Class.forName(DBInf.getDriverClassName());                     java.sql.Connection con=DriverManager.getConnection(DBInf.getMester());                     java.sql.PreparedStatement reStmt=con.prepareStatement(reSQL);                     reStmt.setString(1,path);//path必须是绝对路径                     if(!reStmt.execute())over="恢复成功";                     else over="恢复失败";                     reStmt.close();                     con.close(); } 恢复的时候,有点烦,要关闭所有与数据库的连接,这里我连的是mestar,其实可以 jdbc:microsoft:sqlserver://localhost:1433;user=username;password=pass 连接数据库服务器,不连指定的数据库.这样也可以.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值