学习进度条:
目标任务:
完成多条件查询
预计时间:
2天
完成情况:
完成了查找到实现
代码:
lunwen.java
package bean; public class lunwen { private String title; private String link; private String Abstract; private String zuozhe; private String time; private String keyword; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getLink() { return link; } public void setLink(String link) { this.link = link; } public String getAbstract() { return Abstract; } public void setAbstract(String abstract1) { Abstract = abstract1; } public String getZuozhe() { return zuozhe; } public void setZuozhe(String zuozhe) { this.zuozhe = zuozhe; } public String getTime() { return time; } public void setTime(String time) { this.time = time; } public String getKeyword() { return keyword; } public void setKeyword(String keyword) { this.keyword = keyword; } public lunwen(String title, String link, String abstract1, String zuozhe, String time, String keyword) { super(); this.title = title; this.link = link; Abstract = abstract1; this.zuozhe = zuozhe; this.time = time; this.keyword = keyword; } public lunwen() { } }
dao.java
package dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import DBUtil.DButil; import bean.lunwen; import java.util.LinkedHashMap; import bean.demBean; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import bean.HotWord; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.stream.Collectors; public class dao { public List<lunwen> getsize() throws SQLException{ List<lunwen> list=new ArrayList<>(); lunwen bean=null; String sql="select * from result limit 2"; Connection conn =DButil.getCon(); Statement st=null; ResultSet rs=null; try { st=conn.createStatement(); st.executeQuery(sql); rs=st.executeQuery(sql); while(rs.next()) { String Title=rs.getString("title"); String link = rs.getString("link"); String Abstract= rs.getString("abstract"); String zuozhe = rs.getString("zuozhe"); String time= rs.getString("time"); String keyword= rs.getString("keywork"); bean = new lunwen(Title, link, Abstract, zuozhe, time,keyword); list.add(bean); } } catch (SQLException e) { e.printStackTrace(); } finally{ DButil.close(st, conn); } return list; } public List<lunwen> getselectlunwen(List<demBean> dl) throws SQLException{ List<lunwen> list=new ArrayList<>(); lunwen bean=null; String sql="select * from result where "; for(int i=0;i<dl.size();i++) { if(dl.get(i).getXuanze().equals("题目")) { dl.get(i).setXuanze("title"); } else if(dl.get(i).getXuanze().equals("摘要")) { dl.get(i).setXuanze("abstract"); } else if(dl.get(i).getXuanze().equals("关键词")) { dl.get(i).setXuanze("title"); } else if(dl.get(i).getXuanze().equals("作者")) { dl.get(i).setXuanze("zuozhe"); } } for(int i=0;i<dl.size();i++) { if(i==0) { sql=sql+" "+dl.get(i).getXuanze()+" "; } if(dl.get(i).getAndorno().endsWith("NOT")) { if(dl.get(i).getJingmo().equals("准确查询")) { sql=sql+"and "+dl.get(i).getXuanze()+" "+"!='"+dl.get(i).getNeirong()+"' "; } else if(dl.get(i).getJingmo().equals("模糊查询")) { sql=sql+"and "+dl.get(i).getXuanze()+" "+"not like '%"+dl.get(i).getNeirong()+"%' "; } } if(i!=0&&!dl.get(i).getAndorno().equals("NOT")) { sql=sql+dl.get(i).getAndorno()+" "+dl.get(i).getXuanze()+" "; } if(!dl.get(i).getAndorno().equals("NOT")&&dl.get(i).getJingmo().equals("准确查询")) { sql=sql+"='"+dl.get(i).getNeirong()+"' "; } else if(!dl.get(i).getAndorno().equals("NOT")&&dl.get(i).getJingmo().equals("模糊查询")) { sql=sql+"like '%"+dl.get(i).getNeirong()+"%' "; } } sql=sql+" order by time desc "; System.out.println("SQL="+sql); Connection conn =DButil.getCon(); Statement st=null; ResultSet rs=null; try { st=conn.createStatement(); st.executeQuery(sql); rs=st.executeQuery(sql); while(rs.next()) { String Title=rs.getString("title"); String link = rs.getString("link"); String Abstract= rs.getString("abstract"); String zuozhe = rs.getString("zuozhe"); String time= rs.getString("time"); String keyword= rs.getString("keywork"); bean = new lunwen(Title, link, Abstract, zuozhe, time,keyword); list.add(bean); } } catch (SQLException e) { e.printStackTrace(); } finally{ DButil.close(st, conn); } return list; } public static Map<String,Integer> getrc() { int tnumi=0; String sql="select * from result "; Map<String, Integer>map= new HashMap<String, Integer>(); Map<String, Integer>results= new LinkedHashMap<String, Integer>(); Connection conn =DButil.getCon(); Statement st=null; ResultSet rs=null; try { st=conn.createStatement(); st.executeQuery(sql); rs=st.executeQuery(sql); while(rs.next()) { String keywords=rs.getString("title"); keywords=keywords.substring(1, keywords.length()); String[] split = keywords.split(" "); for(int i=0;i<split.length;i++) { if(map.get(split[i])==null) { map.put(split[i],1); } else { map.replace(split[i], map.get(split[i])+1); } } tnumi++; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } DButil.close(rs, st, conn); map.entrySet() .stream() .sorted((p1, p2) -> p2.getValue().compareTo(p1.getValue())) .collect(Collectors.toList()) .forEach(ele -> results.put(ele.getKey(), ele.getValue())); return results; } }
DBUtil.java
package DBUtil; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DButil { private static String mysqlname = "lunwen"; private static Connection con; private static Statement sta; private static ResultSet re; private static String coursename = "com.mysql.cj.jdbc.Driver"; private static String url = "jdbc:mysql://localhost:3306/"+mysqlname+"?serverTimezone=UTC"; //注册驱动 public static Connection getCon() { try { Class.forName(coursename); System.out.println("驱动加载成功"); }catch(ClassNotFoundException e) { e.printStackTrace(); } try { con = DriverManager.getConnection(url,"root","123asd..00"); System.out.println("连接成功"); }catch(Exception e){ e.printStackTrace(); con = null; } return con; } public static void close (Statement state, Connection conn) { if (state != null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close (ResultSet rs, Statement state, Connection conn) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (state != null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void main(String[] args) { getCon(); } }
遇到问题: