顶会热词后端dao层:
package dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Random; import java.util.Set; import java.util.TreeMap; import java.util.TreeSet; import java.util.regex.Matcher; import java.util.regex.Pattern; import bean.keyword; import bean.paper; public class dao { public Connection getConnection() { try { Class.forName("com.mysql.cj.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } String url = "jdbc:mysql://localhost:3306/paper?serverTimezone=GMT%2B8"; String username = "root"; String password="123456"; Connection a=null; try { a = DriverManager.getConnection(url,username,password); } catch (SQLException e) { e.printStackTrace(); } return a; } public List<paper>getallpaper() throws NumberFormatException, SQLException//获取所有论文数据 { List<paper> list=new ArrayList<paper>(); Connection coon=getConnection(); String sql="select* from pypaper"; PreparedStatement pa=coon.prepareStatement(sql); ResultSet rs=pa.executeQuery(sql); while(rs.next()) { paper p; try { p = new paper(Integer.parseInt(rs.getString("id")),rs.getString("name"),rs.getString("herf"),rs.getString("writer"),rs.getString("Abstract"),rs.getString("time"),rs.getString("keywords")); list.add(p); } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } rs.close(); pa.cancel(); coon.close(); return list; } public List<paper>findpaper(String id,String name,String writer,String time,String keyword) throws SQLException//模糊查询论文 { List<paper> list=new ArrayList<paper>(); Connection coon=getConnection(); String sql="select * from pypaper"; //拼接模糊查询sql语句 if(id.length()>0) sql=sql+" where id = '"+id+"'"; if(name.length()>0&&sql.length()>21) sql=sql+" and name like '%"+name+"%'"; if(name.length()>0&&sql.length()==21) sql=sql+" where name like '%"+name+"%'"; if(writer.length()>0&&sql.length()>21) sql=sql+" and writer like '%"+writer+"%'"; if(writer.length()>0&&sql.length()==21) sql=sql+" where writer like '%"+writer+"%'"; if(time.length()>0&&sql.length()>21) sql=sql+" and time like '%"+time+"%'"; if(time.length()>0&&sql.length()==21) sql=sql+" where time like '%"+time+"%'"; if(keyword.length()>0&&sql.length()>21) sql=sql+" and keywords like '%"+keyword+"%'"; if(keyword.length()>0&&sql.length()==21) sql=sql+" where keywords like '%"+keyword+"%'"; //System.out.println(sql); PreparedStatement pa=coon.prepareStatement(sql); ResultSet rs=pa.executeQuery(sql); while(rs.next()) { paper p; try { p = new paper(Integer.parseInt(rs.getString("id")),rs.getString("name"),rs.getString("herf"),rs.getString("writer"),rs.getString("Abstract"),rs.getString("time"),rs.getString("keywords")); list.add(p); } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } rs.close(); pa.cancel(); coon.close(); return list; } public List<keyword>getkey(String id,String name,String writer,String time,String keyword) throws SQLException//获取关键词 { List<keyword> list=new ArrayList<keyword>(); Connection coon=getConnection(); String sql="select* from pypaper"; if(id.length()>0) sql=sql+" where id = '"+id+"'"; if(name.length()>0&&sql.length()>21) sql=sql+" and name like '%"+name+"%'"; if(name.length()>0&&sql.length()==21) sql=sql+" where name like '%"+name+"%'"; if(writer.length()>0&&sql.length()>21) sql=sql+" and writer like '%"+writer+"%'"; if(writer.length()>0&&sql.length()==21) sql=sql+" where writer like '%"+writer+"%'"; if(time.length()>0&&sql.length()>21) sql=sql+" and time like '%"+time+"%'"; if(time.length()>0&&sql.length()==21) sql=sql+" where time like '%"+time+"%'"; if(keyword.length()>0&&sql.length()>21) sql=sql+" and keywords like '%"+keyword+"%'"; if(keyword.length()>0&&sql.length()==21) sql=sql+" where keywords like '%"+keyword+"%'"; PreparedStatement pa=coon.prepareStatement(sql); ResultSet rs=pa.executeQuery(sql); String TEMP=""; while(rs.next()) { TEMP+=rs.getString("keywords").replace('+', ' '); } int P=30; TEMP=TEMP.replace('+', ' ');//将关键词字符串的+替换为空格 list=countToEng(TEMP);//对热词排序并生成keyword的list对象 return list; } // 统计英文单词 public List<keyword> countToEng(String str) {//生成keyword的list对象 List<keyword> arraykey=new ArrayList<keyword>(); // 定义返回数组 String[] re_str = new String[30]; // 定义ArrayList对象存储匹配到的单词 ArrayList<String> array = new ArrayList<>(); // 使用正则获取单词 Pattern pattern = Pattern.compile("\b[\w+\-']+\b"); Matcher matcher = pattern.matcher(str); while (matcher.find()) { array.add(matcher.group()); } // 定义Map集合存储单词,键为单词不重复,值为统计的数量 TreeMap<String, Integer> map = new TreeMap<String, Integer>(); // 遍历字符数组,获取到每一个字符 for (String tstr : array) { // 用每一个字符作为键,在TreeMap中查找 Integer val = map.get(tstr); if (val == null) { // 返回null,则不存在,存储1 map.put(tstr, 1); //System.out.println("不存在:"+tstr); } else { // 返回非null,则把值加1,重新存储 val++; map.put(tstr, val); } } // // 获取键值对的Set集合 Set<Map.Entry<String, Integer>> sme = map.entrySet(); // 遍历拼接 for (Map.Entry<String, Integer> me : sme) { keyword k=new keyword(); k.setName(me.getKey()); k.setValue(me.getValue()); arraykey.add(k); } arraykey=maopao(arraykey);//排序 return arraykey; } public List<keyword> maopao(List<keyword> arr)//冒泡排序 { for(int i=0;i<arr.size()-1;i++) { int a=i; keyword max=arr.get(i); for(int j=i+1;j<arr.size();j++) { if(max.getValue()<arr.get(j).getValue()) { a=j; max=arr.get(j); } } arr.set(a, arr.get(i)); arr.set(i, max); if(i==arr.size()-10) break; } return arr; } }