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