Java执行mysql具体表中的具体数据备份

    /**
     * 数据备份
     * savePath 是路径
     * type 是类型
     */
    @RequestMapping("/dataBackUp")
    @ResponseBody
    public void dataBackUp(String savePath, int type) throws IOException {
        File saveFile = new File(savePath);
        if (!saveFile.exists()) {// 如果目录不存在  
            saveFile.mkdirs();// 创建文件夹  
        }
        if (!savePath.endsWith(File.separator)) {
            savePath = savePath + File.separator;
        }
        if (dataBackUps(savePath, type)) {
            System.out.println("数据库备份成功!!!");
  
        } else {
            System.out.println("数据库备份失败!!!");
         
        }

    }


private static boolean dataBackUps(String savePath, int type) throws IOException {
        StringBuilder command  = new StringBuilder();
        Runtime rt = Runtime.getRuntime();
        Process pro = null;
        command.append("mysqldump --opt -h 192.168.3.27 -u" + username + " -p" + password + " test ");
        String time = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(new Date());
        String fileName =savePath +"/" + time + ".sql";

        command.append("agricultural");
        command.append(" ");
        command.append("-w \"  typeNumber="+type);
        command.append("\"");
        command.append(" ");
        command.append("--result-file=" + fileName+" --default-character-set=utf8");
        pro = rt.exec(command.toString());

        //命令执行出错则打印相关错误信息
        BufferedReader br = new BufferedReader(new InputStreamReader(	pro.getErrorStream()));
        String errorLine = null;
        while ((errorLine = br.readLine()) != null) {
            System.out.println(errorLine);
        }
        br.close();
        return true;
    }

在上面中192.168.3.27是mysql所在的ip地址,username 和password是链接数据库的用户名和密码,test 是数据库名,agricultural是表名,"-w " typeNumber="+type 表示在表中的type Number字段等于type的数据.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值