• 定时查询数据库某一字段为空(MySQL)


    用java+sql定时对MySQL进行查询,将“pace_name”为空的行提取出来

    PicDao.java

    package example.dao;
    
    import example.bean.PicBean;
    import example.util.DBUtil;
    import org.apache.commons.lang3.StringUtils;
    
    import java.sql.*;
    import java.util.ArrayList;
    import java.util.List;
    
    public class PicDao {
    
        //通过路径获取图片信息
        public static PicBean GetThePicByPath(String path) {
            String sql = "select * from pic where pic_path = "+path;
            Connection conn = DBUtil.getConnection();
            Statement state = null;
            ResultSet rs = null;
            PicBean test = new PicBean();
            try {
                state = conn.createStatement();
                rs = state.executeQuery(sql);
                while (rs.next()) {
                    int id  = rs.getInt("id");
                    String username = rs.getString("username");
                    String pic_path = rs.getString("pic_path");
                    String pic_name = rs.getString("pic_name");
                    String pic_type = rs.getString("pic_type");
                    int pic_type_number = rs.getInt("pic_type_number");
                    String pic_time = rs.getString("pic_time");
                    String pic_position_sheng = rs.getString("pic_position_sheng");
                    String pic_position_shi = rs.getString("pic_position_shi");
                    String pic_position_xian = rs.getString("pic_position_xian");
                    String facename = rs.getString("face_name");
    
                    test.setId(id);
                    test.setUsername(username);
                    test.setPic_name(pic_name);
                    test.setPic_path(pic_path);
                    test.setPic_type(pic_type);
                    test.setPic_type_number(pic_type_number);
                    test.setPic_time(pic_time);
                    test.setPic_position_sheng(pic_position_sheng);
                    test.setPic_position_shi(pic_position_shi);
                    test.setPic_position_xian(pic_position_xian);
                    test.setFace_name(facename);
    
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                DBUtil.close(rs, state, conn);
            }
            return test;
        }
    
        //取出所有facename为空的行
        public static List<PicBean> ListFaceNameNULL() {
            String sql = "select * from pic";
            List<PicBean> test = new ArrayList<>();
            Connection conn = DBUtil.getConnection();
            Statement state = null;
            ResultSet rs = null;
            try {
                state = conn.createStatement();
                rs = state.executeQuery(sql);
                while (rs.next()) {
    
                    String pic_type = rs.getString("pic_type");
                    String facename = rs.getString("face_name");
                    //System.out.println(pic_type+" "+facename);
    
                    if(pic_type.equals("人") && StringUtils.isBlank(facename)){
    
                        int id  = rs.getInt("id");
                        String username = rs.getString("username");
                        String pic_path = rs.getString("pic_path");
                        String pic_name = rs.getString("pic_name");
                        int pic_type_number = rs.getInt("pic_type_number");
                        String pic_time = rs.getString("pic_time");
                        String pic_position_sheng = rs.getString("pic_position_sheng");
                        String pic_position_shi = rs.getString("pic_position_shi");
                        String pic_position_xian = rs.getString("pic_position_xian");
    
                        PicBean test1 = new PicBean();
                        test1.setId(id);
                        test1.setUsername(username);
                        test1.setPic_name(pic_name);
                        test1.setPic_path(pic_path);
                        test1.setPic_type(pic_type);
                        test1.setPic_type_number(pic_type_number);
                        test1.setPic_time(pic_time);
                        test1.setPic_position_sheng(pic_position_sheng);
                        test1.setPic_position_shi(pic_position_shi);
                        test1.setPic_position_xian(pic_position_xian);
                        test1.setFace_name(facename);
    
                        System.out.println("facename为空的id:"+id);
                        test.add(test1);
    
                    }
    
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                DBUtil.close(rs, state, conn);
            }
            //System.out.println(test);
            return test;
        }
    
        //给facename打标签
        public static boolean ChangeTheFaceName(int id, String facename) {
            Connection conn = DBUtil.getConnection();
            boolean flag = false;
    
            try {
                String sql = "update pic set face_name = '"+facename + "' where id = '"+id+"'";
    
                System.out.println(sql);
                PreparedStatement pstmt = conn.prepareStatement(sql);
                int i = pstmt.executeUpdate();
                pstmt.close();
                conn.close();
                if(i>0) {
                    flag = true;
                }
    
            } catch (SQLException e) {
                e.printStackTrace();
            }
    
            return flag;
        }
    
    }

    index.java

    package example.total;
    
    import example.bean.PicBean;
    import example.dao.FaceDao;
    import example.dao.PicDao;
    
    import java.util.List;
    
    public class index {
        public static void main(String[] args) throws InterruptedException {
    
            for(int i = 0; i < 10; i++){
    
                List<PicBean> pic = PicDao.ListFaceNameNULL();
    
                //判断是否存在facename为空的行
                if (pic.isEmpty()){
                    System.out.println("执行第"+ i+1 +"次!");
                    Thread.sleep(5000);
                }else{
    
                    System.out.println("执行第"+ i+1 +"次!");
    
                    //System.out.println(pic.size());
                    //逐个将facename插入到表中
                    for(int j = 0; j < pic.size(); j++){
    
                        int id = pic.get(j).getId();
                        String path = pic.get(j).getPic_path();
                        System.out.println(id+":"+path);
    
                        String facename = FaceDao.searchface(path);
                        System.out.println(facename);
    
                        PicDao.ChangeTheFaceName(id,facename);
                    }
    
                }
            }
    
        }
    }
  • 相关阅读:
    1. Spring Cloud Greenwich SR2 概览
    Spring zuul 快速入门实践 --服务转发实现解析
    文件上传下载原理:http协议分析及实现
    Tomcat session的实现:线程安全与管理
    Dubbo(七):redis注册中心的应用
    Dubbo(六):zookeeper注册中心的应用
    Dubbo(五):集群容错的实现
    Dubbo(四):服务路由的实现
    Dubbo(三):负载均衡实现解析
    Nginx(一):启动流程解析
  • 原文地址:https://www.cnblogs.com/vvxvv/p/14911192.html
Copyright © 2020-2023  润新知