java 备份mssql

http://www.111cn.net/jsp/Java/33832.htm

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

更多详细内容请查看:http://www.111cn.net/jsp/Java/33832.htm

你可能感兴趣的:(java,sql,jsp,Web,.net)