ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

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

2021-06-21 09:36:13  阅读:171  来源: 互联网

标签:某一字 String getString rs pic 查询数据库 MySQL position type


用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);
                }

            }
        }

    }
}

 

标签:某一字,String,getString,rs,pic,查询数据库,MySQL,position,type
来源: https://www.cnblogs.com/vvxvv/p/14911192.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有