zoukankan      html  css  js  c++  java
  • Java---编写利用runtime命令实现自动备份数据库

    runtime可实现调用系统命令打开网页

    package day;
    
    import java.io.IOException;
    
    public class Runt {
        public static void main(String[] args) {
            Runtime run = Runtime.getRuntime();
            System.out.println("jvm最大内存量为"+ run.maxMemory());
            try {
                run.exec("cmd /c start http://www.baidu.com");    //打开百度
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    实现数据库的定时备份(利用多线程)

    package day;
    
    import java.io.File;
    import java.io.IOException;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Date;
    
    public class Db implements Runnable {
        private int m = 0;
    
        public Db(int m) {
            this.m = m;
        }
    
        public Db() {
            this.m = 5;
        }
    
        @Override
        public void run() {
            long ftime = 0;
            int num = 0;
            long start = 0;
            while (true) {
                start += 5;
                SimpleDateFormat sd = new SimpleDateFormat("yyyHHddHHmmss");
                Date d = new Date();
                String ff = sd.format(d) + ".sql";
                Runtime run = Runtime.getRuntime();
                try {
                    if (start >= (this.m * 60)) {                       //m分钟后开始备份
                        run.exec("cmd /c D:\m8\mysql-8.0.12\bin\mysqldump.exe -uroot -proot zzz zzz > c:\dbtext\" + ff);     //使用runtime实现数据库备份zzz数组库
                        start = 0;
                        num++;
                        while (num > 3) {                               //文件夹中只存放最近三次的备份,多余则删除
                            File[] f = new File("c:/dbtext").listFiles();
                            var arr = new ArrayList<Long>();
                            for (File fl : f) {
                                ftime = fl.lastModified();
                                arr.add(ftime);
                            }
                            long min = Collections.min(arr);
                            for (File fl : f) {
                                if (fl.lastModified() == min) {
                                    fl.delete();
                                }
                            }
                            num--;
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    -----------------------------------------------------------------------------
    
    package day;
    public class Dbtext {
        public static void main(String[] args) {
            Db d = new Db(1);
            Thread th = new Thread(d);                                            //开启线程一直执行
            th.start();
        }
    }
  • 相关阅读:
    读取组合单元格
    Spire.XLS:一款Excel处理神器
    linq
    LINQ语句中的.AsEnumerable() 和 .AsQueryable()的区别
    合并单元格
    web sec / ssd / sshd
    linux——cat之查看cpu信息、显示终端、校验内存.............
    MATLAB mcr lib的环境变量书写
    Linux查看库依赖方法
    判断当前所使用python的版本和来源
  • 原文地址:https://www.cnblogs.com/zxwen/p/9675153.html
Copyright © 2011-2022 走看看