java备份数据库四种方法(mysql,mssql数据库备份)

java备份数据库教程四种方法(mysql教程,mssql数据库备份)
mysql数据库备份的东西,然后研究了一下java语言中怎么来调用cmd的命令来实现mysqldump的备份功能。具体实现如下:

1.首先设置环境变量:有两种方式。第一种,在环境变量中添加 mysql_home,设置内容为 mysql的安装路径,然后,在path中添加路径  %mysql_home%bin    。第二种方法,不新建mysql_home,而是直接在path中添加   mysql安装路径bin 。这样,调用cmd的时候可以直接找到mysqldump和mysql命令了。

2.现在编写java方法类来实现数据库的备份和还原。代码如下:
import java.io.ioexception;

public class db_deal {       

        private static string str=null;       

        public static void backup(){
                //使用mysqldump来备份数据库,格式"mysqldump -u username -ppassword --opt database_name > direction/backup_name.sql"
                str="mysqldump -u root -proot --opt   hjh > d:/test.sql";   
                try { 
                        runtime rt=runtime.getruntime();
                        rt.exec("cmd /c"+str);
                //runtime.getruntime().exec(   )这个方法可以实现对命令的调用。具体内容看api
                //上面可以cmd调用控制台,然后执行str中的字符串表示的命令。
                        system.out.println("successly!");
                } catch (ioexception e) { 
                        e.printstacktrace(); 
                        system.out.println("something wrong!");               
           
                } 
               
        }
       
        public static void load(){
                str="mysql -u root -proot j2603  <  d:/test.sql";
                // mysql命令可以实现数据库的还原。格式"mysql -u username  -ppassword   database_name     <     back_up_dir  "
                runtime rt=runtime.getruntime();
                try {
                        rt.exec("cmd /c"+str);
                        system.out.println("restore successly!");
                } catch (ioexception e) {
                        e.printstacktrace();
                        system.out.println("restore fail!");
                }
        }
}

3.最后要备份的地方给这个类实例化,然后调用backup(),load()来实现mysql数据库的简单备份跟还原

//下面来看看备份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
连接数据库服务器,不连指定的数据库.这样也可以

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值