结对开发-热词云统计
getci.java
package ciyun; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/getci") public class getci extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); List<yun1>list=new ArrayList<yun1>(); Dao dao=new Dao(); list=dao.search1(); if (list!=null) { System.out.print("111"); request.setAttribute("list", list); request.getRequestDispatcher("show.jsp").forward(request, response); } else { System.out.print("222"); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
yun1.java
package ciyun; public class yun1 { private String keywords; private String href; public yun1() { super(); // TODO Auto-generated constructor stub } public yun1(String keywords,String href) { super(); this.keywords=keywords; this.href=href; // TODO Auto-generated constructor stub } public String getKeywords() { return keywords; } public void setKeywords(String keywords) { this.keywords = keywords; } public String getHref() { return href; } public void setHref(String href) { this.href = href; } @Override public String toString() { return "yun1 [keywords=" + keywords + ", href=" + href + "]"; } }
Dao.java
package ciyun; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import com.mysql.jdbc.Statement; //import com.sun.org.apache.xpath.internal.operations.And; public class Dao { public List<yun1> search1(){ int num=0; List<yun1>list=new ArrayList<yun1>(); Connection con=null; java.sql.Statement psts=null; ResultSet rs=null; try { con=DBUtils.getCon(); String sql="select * from lunwen_table "; System.out.print(sql); psts=con.createStatement(); rs=psts.executeQuery(sql); while(rs.next()){ //把数据库里面的数值传到界面 String keywords=rs.getString("keywords"); String href=rs.getString("href"); yun1 c=new yun1(keywords,href);//通过构造函数进行赋值 list.add(c);//将赋值的数添加到这个集合中 } } catch (SQLException e) { e.printStackTrace(); } return list; } public static void main(String[] args) { Dao dao=new Dao(); } }
DBUtils.java
package ciyun; import java.beans.Statement; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; public class DBUtils { private static String mysqlname = "dalaode"; private static Connection con; private static Statement sta; private static ResultSet re; private static String coursename = "com.mysql.jdbc.Driver"; private static String url = "jdbc:mysql://localhost:3306/"+mysqlname+"?useSSL=false&characterEncoding=utf8"; //娉ㄥ唽椹卞姩 public static Connection getCon() { try { Class.forName(coursename); System.out.println("用户名"); }catch(ClassNotFoundException e) { e.printStackTrace(); } try { con = DriverManager.getConnection(url,"root","123456"); System.out.println("连接数据库"); }catch(Exception e){ e.printStackTrace(); con = null; } return con; } public static void close(Statement sta,Connection connection) { if(sta!=null) { try { ((Connection) sta).close(); }catch(SQLException e) { e.printStackTrace(); } } if(connection!=null) { try { connection.close(); }catch(SQLException e) { e.printStackTrace(); } } } //鍏抽棴杩炴帴 public static void close(ResultSet re,Statement sta,Connection connection) { if(re!=null) { try { re.close(); }catch(SQLException e) { e.printStackTrace(); } } if(sta!=null) { try { ((Connection) sta).close(); }catch(SQLException e) { e.printStackTrace(); } } if(connection!=null) { try { connection.close(); }catch(SQLException e) { e.printStackTrace(); } } } public static void main(String[] args) { getCon(); } }
show.jsp
<%@page language="java" contentType="textml; charset=UTF-8" pageEncoding="UTF-8"%> <%@page import="java.util.*"%> <%@page import="ciyun.yun1"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>热词展示</title> <script src="https://cdn.bootcss.com/wordcloud2.js/1.1.0/wordcloud2.js"></script> <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script> </head> <body> <% request.setCharacterEncoding("utf-8"); List <yun1> keywords =(List<yun1>) request.getAttribute("list"); List <yun1> href =(List<yun1>) request.getAttribute("list"); String str=" "; for(yun1 keyword:keywords) { str=keyword.getKeywords()+str; } String[] lianjie=new String[10000]; int h=0; for(yun1 href1:href) { //lianjie=href1.getHref()+lianjie; lianjie[h]= href1.getHref(); h++; } System.out.print(lianjie[0]); String[] words = str.split(" "); // 非单词的字符来分割,得到所有单词split 方法 Map<String ,Integer> map = new HashMap<String, Integer>() ; for(String word :words){ if(map.get(word)==null){ // 若不存在说明是第一次,则加入到map,出现次数为1 map.put(word,1); }else{ map.put(word,map.get(word)+1); // 若存在,次数累加1 } } // 排序 List<Map.Entry<String ,Integer>> list = new ArrayList<Map.Entry<String,Integer>>(map.entrySet()); //在java中,如果要对集合对象或数组对象进行排序,需要实现Comparator接口以达到我们想要的目标 Comparator<Map.Entry<String,Integer>> comparator = new Comparator<Map.Entry<String, Integer>>() { public int compare(Map.Entry<String, Integer> left, Map.Entry<String, Integer> right) { return (left.getValue().compareTo(right.getValue())); } }; // 集合默认升序升序 Collections.sort(list,comparator); String ten[]=new String[30]; int shu[]=new int[30]; for(int i=0;i<30;i++){// 由高到低输出 ten[i]=list.get(list.size()-i-1).getKey(); shu[i]=list.get(list.size()-i-1).getValue(); System.out.println(list.get(list.size()-i-1).getKey() +":"+list.get(list.size()-i-1).getValue()); } %> <div id="canvas-container" align="center"> <canvas id="canvas" width="800px" height="600px"></canvas> </div> <script> var options = eval({ "list": [ ['<%=ten[2] %>', <%=shu[2]%>], ['<%=ten[3] %>', <%=shu[3]%>], ['<%=ten[4] %>', <%=shu[4]%>], ['<%=ten[6] %>', <%=shu[6]%>], ['<%=ten[7] %>', <%=shu[7]%>], ['<%=ten[8] %>', <%=shu[8]%>], ['<%=ten[9] %>', <%=shu[9]%>], ['<%=ten[11] %>', <%=shu[11]%>], ['<%=ten[13] %>', <%=shu[13]%>], ['<%=ten[14] %>', <%=shu[14]%>], ['<%=ten[15] %>', <%=shu[15]%>], ['<%=ten[16] %>', <%=shu[16]%>], ['<%=ten[17] %>', <%=shu[17]%>], ['<%=ten[18] %>', <%=shu[18]%>], ['<%=ten[19] %>', <%=shu[19]%>], ['<%=ten[20] %>', <%=shu[20]%>], ['<%=ten[21] %>', <%=shu[21]%>], ['<%=ten[22] %>', <%=shu[22]%>], ['<%=ten[23] %>', <%=shu[23]%>], ['<%=ten[25] %>', <%=shu[25]%>], ['<%=ten[26] %>', <%=shu[26]%>], ['<%=ten[27] %>', <%=shu[27]%>], ['<%=ten[28] %>', <%=shu[28]%>], ['<%=ten[29] %>', <%=shu[29]%>], ], "gridSize": 13, "weightFactor": 1, "fontWeight": 'normal', "fontFamily": 'Times, serif', "color": 'random-light', "backgroundColor": '#333', "rotateRatio": 1 }); var canvas = document.getElementById('canvas'); WordCloud(canvas, options); </script> <div> <table> <th>论文连接</th> <tr> <td><a href="<%=lianjie[0] %>"><%=lianjie[0] %></a></td> </tr> <tr> <td><a href="<%=lianjie[20] %>"><%=lianjie[20] %></a></td> </tr> <tr> <td><a href="<%=lianjie[30] %>"><%=lianjie[20] %></a></td> </tr> <tr> <td><a href="<%=lianjie[34] %>"><%=lianjie[20] %></a></td> </tr> <tr> <td><a href="<%=lianjie[36] %>"><%=lianjie[20] %></a></td> </tr> </table> </div> </body> <html>
运行截图:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律