线程方式定时删除本地文件和数据库记录写法

代码:

import java.io.File;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;

public class DeleteImageAtSpecifiedTime implements Runnable {
    static Logger logger = Logger.getLogger(DeleteImageAtSpecifiedTime.class);
    /**
     * 判断指定的文件删除是否成功
     * 
     * @param FileName 文件路径
     * @return true or false 成功返回true,失败返回false
     */
    public static boolean deleteFile(File file) {
//        File file = new File(fileName);// 根据指定的文件名创建File对象
        if (file.exists() && file.isFile()) { // 要删除的文件存在且是文件
            if (file.delete()) {
                logger.info("图片删除成功!");
                return true;
            } else {
                logger.info("图片删除失败!");
                return false;
            }
        } else {
            logger.info("图片不存在,删除失败!");
            return false;
        }
    }

    @SuppressWarnings("static-access")
    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(1000 * 60 * 60);//每小时运行一次
                logger.info("定时删除本地图片线程休眠结束.........");
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            String sql = null;
            Connection connection = null;
            Statement statement = null;
            ResultSet resultSet = null;
            Date date = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = dateFormat.format(date);
            JdbcUtil jdbcUtil = new JdbcUtil();
            sql = "SELECT imagePath FROM 数据库 WHERE DATE_ADD(createTime,INTERVAL 30 DAY) < NOW()";//判断当前时间大于创建时间30天的
            logger.info("待执行的SQL:"+sql);
            try {
                connection = jdbcUtil.getConnection();
                statement = connection.createStatement();
                resultSet = statement.executeQuery(sql);
                while (resultSet.next()) {
                    String imagePath = resultSet.getString("imagePath");//这里是得到数据库中本地图片的绝对路径
                    File file = new File(imagePath);
                    boolean boon = deleteFile(file);//调用删除方法
                    if(boon==true) {//判断本地删除成功之后再删除数据库的数据
                        sql="DELETE FROM 数据库 WHERE DATE_ADD(createTime,INTERVAL 6 DAY) < NOW()";
                        logger.info("待执行的SQL:"+sql);
                        int executeUpdate = statement.executeUpdate(sql);
                        logger.info(executeUpdate+"行记录被删除");
                    }
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

启动线程:

public static void main(String []args){
    Runnable runnable = new DeleteImageAtSpecifiedTime();
    Thread thread = new Thread(runnable);
    thread.start();
    logger.info("定时删除本地图片线程启动.........");
}
createTime字段数据库中设计默认位置应该填写CURRENT_TIMESTAMP,
CURRENT_TIMESTAMP监控当前数据如果有更新,此createTime字段就会自动创建一个更新时间互相对应。

关于以上方法中的工具类在我以前的随笔中有记录,可以找找看。
原文地址:https://www.cnblogs.com/wangquanyi/p/11446137.html