zoukankan      html  css  js  c++  java
  • 结对开发-热词云统计

    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>

    运行截图:

  • 相关阅读:
    mysql基础(三)存储引擎和锁
    为给定字符串生成MD5指纹
    区块链基本原理,Part-2:工作量证明和权益证明
    区块链基本原理,Part-1:拜占庭容错
    区块链挖矿 2.0
    以太坊 2.0 中的验证者经济模型,Part-2
    以太坊 2.0 中的验证者经济模型,Part-1
    Java归并排序之递归
    Python爬虫入门教程 64-100 反爬教科书级别的网站-汽车之家,字体反爬之二
    Java棋盘覆盖问题
  • 原文地址:https://www.cnblogs.com/fengjingfei/p/13061723.html
Copyright © 2011-2022 走看看