做一个项目中的数据库备份与恢复(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 连接数据库服务器,不连指定的数据库.这样也可以.
数据库备份与恢复java代码_JAVA程序中 实现数据库备份与恢复
最新推荐文章于 2024-07-06 02:18:24 发布