个人作业-顶会热词统计(项目部分)
基本框架:

代码:
bean.java
package Bean; public class bean { private String id; private String title; private String keywords; private String link; public String getLink() { return link; } public void setLink(String link) { this.link = link; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getKeywords() { return keywords; } public void setKeywords(String keywords) { this.keywords = keywords; } }
Dao.java
package Dao; import Bean.bean; import DBUtil.DBUtil; import java.util.ArrayList; import java.util.List; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; public class Dao { public List<bean> searchById(String id){ List<bean> list = new ArrayList<bean>(); try { Connection conn = DBUtil.getConn(); Statement state = null; String sql="select * from ppp where id=?"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1,id); ResultSet rs = pstmt.executeQuery(); System.out.println("Dao成功运行************************"); while(rs.next()){ bean lu = new bean(); lu.setId(rs.getString("id")); lu.setKeywords(rs.getString("keywords")); lu.setTitle(rs.getString("title")); lu.setLink(rs.getString("link")); list.add(lu); } rs.close(); pstmt.close(); conn.close(); }catch(SQLException e) { System.out.println("发生错误"); e.printStackTrace(); } return list; } public List<bean> search(String id,String title,String keywords){ List<bean> list = new ArrayList<bean>(); try { Connection conn = DBUtil.getConn(); Statement state = null; String sql = "select * from ppp where id like '%"+id+"%' and title like '%"+title+"%' and keywords like '%"+keywords+"%'"; PreparedStatement pstmt = conn.prepareStatement(sql); ResultSet rs = pstmt.executeQuery(); System.out.println("Dao成功运行************************"); while(rs.next()){ bean lu = new bean(); lu.setId(rs.getString("id")); lu.setKeywords(rs.getString("keywords")); lu.setTitle(rs.getString("title")); lu.setLink(rs.getString("link")); list.add(lu); } rs.close(); pstmt.close(); conn.close(); }catch(SQLException e) { System.out.println("发生错误"); e.printStackTrace(); } return list; } public boolean add(bean bean) { Connection conn = DBUtil.getConn(); PreparedStatement pstmt = null; boolean f = false; int a=0; try { String sql = "insert into ppp(title,link) value(?,?)"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, bean.getTitle()); pstmt.setString(2, bean.getLink()); a = pstmt.executeUpdate(); } catch(SQLException e) { e.printStackTrace(); } finally { DBUtil.close(pstmt, conn); } if(a>0) f=true; return f; } public boolean delete(String id) { boolean flag = false; Connection conn = DBUtil.getConn(); Statement state = null; try { String sql = "delete from ppp where id = '"+id+"'"; PreparedStatement pstmt = conn.prepareStatement(sql); int i = pstmt.executeUpdate(); pstmt.close(); conn.close(); if(i>0) flag = true; } catch (SQLException e) { System.out.println("删除失败!"); e.printStackTrace(); } return flag; } //修改会议室信息 public boolean alter(bean bean,String id) { String sql = "update title set tltle='" + bean.getTitle() + "', link='" + bean.getLink() + "' where id='" + id + "'"; Connection conn = DBUtil.getConn(); Statement state = null; boolean f = false; int a = 0; try { state = conn.createStatement(); System.out.println("看看是不是执行了"); a = state.executeUpdate(sql); System.out.println(a); } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(state, conn); } if (a > 0) { f = true; } System.out.println(f); return f; } public void xiugai1(String id,String title,String link) { Connection conn = DBUtil.getConn(); Statement state =null; ResultSet rs = null; int flag=0; try { String sql = "update ppp set title='"+title+"',link ='"+link+"' where id='"+id+"'"; state = conn.createStatement(); int count = state.executeUpdate(sql); if(count>0) { System.out.println("数据插入成功"); }else { System.out.println("数据插入失败"); } } catch(Exception e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } } }
DBUtil.java
package DBUtil; import java.sql.*; public class DBUtil { static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; //数据库驱动名 static final String url = "jdbc:mysql://localhost:3306/paqu";//数据库地址 static final String user = "root"; static final String password = "123456"; //连接数据库 public static Connection getConn () { Connection conn = null; try { Class.forName("com.mysql.cj.jdbc.Driver");//加载驱动 conn = DriverManager.getConnection(url, user, password); } catch (Exception e) { e.printStackTrace(); } return conn; } //关闭连接 public static void close (PreparedStatement preparedState, Connection conn) { if (preparedState != null) { try { preparedState.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close (ResultSet rs, PreparedStatement preparedState, Connection conn) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (preparedState != null) { try { preparedState.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } /** * 关闭连接 * @param state * @param conn */ 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) throws SQLException { Connection conn = getConn(); PreparedStatement preparedStatement = null; ResultSet rs = null; String sql ="select * from ppp";//数据库名称 preparedStatement = conn.prepareStatement(sql); rs = preparedStatement.executeQuery(); if(rs.next()){ System.out.println("数据库不为空"); } else{ System.out.println("数据库为空"); } } }
Servlet.java
package Servlet; import java.io.IOException; import java.util.List; import javax.el.BeanNameELResolver; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.eclipse.jdt.internal.compiler.IDebugRequestor; import Bean.bean; import Dao.Dao; @WebServlet("/Servlet") public class Servlet extends HttpServlet { private static final long serialVersionUID = 1L; Dao dao = new Dao(); protected void service(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String method = req.getParameter("method"); if("searchbyid".equals(method)) { searchById(req,resp); }else if("search".equals(method)) { search(req,resp); }else if("add".equals(method)) { add(req,resp); }else if("delete".equals(method)) { delete(req,resp); }else if("alter".equals(method)) { alter(req, resp); }else if("alter2".equals(method)) { alter2(req, resp); } } public Servlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().append("Served at: ").append(request.getContextPath()); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } private void searchById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{ // System.out.println(req.getParameter("title")); req.setCharacterEncoding("utf-8"); resp.setContentType("text/html;charset=utf-8"); Dao dao = new Dao(); List<bean> list = dao.searchById((String)req.getParameter("id")); System.out.println("****************"+(String)req.getParameter("id")); req.setAttribute("list", list); req.getRequestDispatcher("result.jsp").forward(req, resp);//转到指定页面 System.out.println("成功运行************************"); } private void search(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{ // System.out.println(req.getParameter("title")); req.setCharacterEncoding("utf-8"); resp.setContentType("text/html;charset=utf-8"); Dao dao = new Dao(); String id = (String)req.getParameter("id"); String title = (String)req.getParameter("title"); String keywords = (String)req.getParameter("keywords"); System.out.println(id+title+keywords); List<bean> list = dao.search(id,title,keywords); req.setAttribute("list", list); req.getRequestDispatcher("result.jsp").forward(req, resp);//转到指定页面 System.out.println("成功运行************************"); } private void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); resp.setContentType("text/html;charset=utf-8"); String title = req.getParameter("title"); String link = req.getParameter("link"); bean bean = new bean(); bean.setLink(link); bean.setTitle(title); if(dao.add(bean)) { req.setAttribute("bean",bean); req.setAttribute("message","添加成功" ); req.getRequestDispatcher("index.jsp").forward(req, resp); }else { req.setAttribute("message","输入不合法,请重新输入" ); req.getRequestDispatcher("add.jsp").forward(req, resp); } } protected void delete(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException { req.setCharacterEncoding("utf-8"); Dao sd = new Dao(); String id = req.getParameter("delete"); if(dao.delete(id)) { req.setAttribute("message","删除成功" ); req.getRequestDispatcher("index.jsp").forward(req, resp); } else { req.setAttribute("message","失败" ); req.getRequestDispatcher("result.jsp").forward(req, resp); } } protected void alter(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException { req.setCharacterEncoding("utf-8"); Dao dao = new Dao(); String id = (String)req.getParameter("alter"); List<bean> list = dao.searchById(id); req.setAttribute("list", list); req.getRequestDispatcher("alter.jsp").forward(req, resp);//转到指定页面 } private void alter2 (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); Dao dao = new Dao(); String id= req.getParameter("id"); String title = req.getParameter("title"); String link= req.getParameter("link"); dao.xiugai1(id,title,link); req.getRequestDispatcher("index.jsp").forward(req, resp); } }
add.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <style> body{ background: url(aifusen.jpg); background-size:100% 100% ; background-attachment: fixed ; } table{ border-spacing: 10px 20px; border : solid 1px red; margin-top:300px; } table tr{ width :100px; } </style> <body> <% Object message = request.getAttribute("message"); if(message!=null && !"".equals(message)){ %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); </script> <%} %> <form action = "Servlet?method=add" method="post" > <table align="center" valign="middle"> <tr> <td>论文题目:</td> <td> <input type = "text" name = "title" id = "title"></td> </tr> <tr> <td>论文链接:</td> <td> <input type = "text" name = "link" id = "link" ></td> </tr> <tr> <td align ="center"><input type= "reset" value = "重置"> </td> <td align ="center"><input type = "submit" value = "增加"> </td> </tr> </table> </form> </body> </html>
alter.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <% Object message = request.getAttribute("message"); if(message!=null && !"".equals(message)){ %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); </script> <%} %> <form action = "Servlet?method=alter2" method="post" > <c:forEach var="u" items="${list}"> <table align="center" valign="middle"> <tr> <td>论文题目:</td> <td> <input type = "text" name = "title" id = "title" placeholder="${u.title}"></td> </tr> <tr> <td>论文链接:</td> <td> <input type = "text" name = "link" id = "link" placeholder="${u.link}"></td> </tr> <input type ="hidden" name= "id" value ="${u.id} "> <tr> <td align ="center"><input type= "reset" value = "重置"> </td> <td align ="center"><input type = "submit" value = "修改"> </td> </tr> </table> </c:forEach> </body> </html>
check.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <style > body{ background: url(aifusen.jpg); background-size:100% 100% ; background-attachment: fixed ; } table{ border-spacing: 10px 20px; border : solid 1px red; margin-top:300px; } table tr{ width :100px; } </style> <body > <form action = "Servlet?method=search" method="post" > <table align="center" valign="middle"> <tr><td> 论文编号:</<td> <td> <input type="text" id="id" name="id"> </td> </tr> <tr> <td>题目:</td> <td> <input type = "text" name = "title" id = "title"></td> </tr> <tr> <td>关键词:</td> <td> <input type = "text" name = "keywords" id = "keywords" ></td> </tr> <tr> <td align ="center"><input type= "reset" value = "重置"> </td> <td align ="center"><input type = "submit" value = "查询"> </td> </tr> </table> </form> </body> </html>
echart.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>可视化图</title> <script src="js/jquery.min.js"></script> <script src="js/echart3.js"></script> <script src="js/echarts-wordcloud.js"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> <style> #main{ width: 100%; height: 1500px; border:1px solid #ddd; float:center; } </style> </head> <body> <div id="main" align="center" style="height:300%;"> </div> <div id="biaoge"> </div> <div id="zhuzhuang" style="height:500%;"> </div> <script type="text/javascript"> var dt; var hzb=new Array(0); var zzb=new Array(0); var str="<table border='1'><tbody><tr><td width='35%'>热词</td> <td width='15%'>数量</td></tr><br>"; var str1="</tbody></table>"; $.ajax({ url : "servlet?method=reci", async : true, type : "POST", data : { }, dataType : "json", success : function(data) { dt = data; var mydata = new Array(0); for (var i = 0; i < dt.length; i++) { var d = {}; d["name"] = dt[i].name; d["value"] = dt[i].value; mydata.push(d); hzb.push(dt[i].name); zzb.push(dt[i].value); var tab=document.getElementById("biaoge"); str=str+"<tr><td>"+dt[i].name+"</td> <td>"+dt[i].value+"</td></tr><br>"; if(i==dt.length){ str=str+str1; } tab.innerHTML = str; } //alert("mydata"+mydata); var myChart = echarts3.init(document.getElementById('main')); //设置点击效果 myChart.setOption({ title: { text: '' }, tooltip: {}, series: [{ type : 'wordCloud', //类型为字符云 shape:'smooth', //平滑 gridSize : 5, //网格尺寸 size : ['50%','50%'], //sizeRange : [ 50, 100 ], rotationRange : [-45, 0, 45, 90,60,16], //旋转范围 textStyle : { normal : { fontFamily:'微软雅黑', color: function() { return 'rgb(' + Math.round(Math.random() * 255) + ', ' + Math.round(Math.random() * 255) + ', ' + Math.round(Math.random() * 255) + ')' } }, emphasis : { shadowBlur : 5, //阴影距离 shadowColor : '#373' //阴影颜色 } }, left: null, top: 'center', right: null, bottom: null, width:'100%', height:'100%', data:mydata }] }); var zhudiv=echarts3.init(document.getElementById('zhuzhuang')); option = { yAxis: { type: 'category', data: hzb }, xAxis: { type: 'value' }, series: [{ data: zzb, itemStyle: { normal: { label: { show: true, position: 'right', textStyle: { color: 'black' } } }, }, type: 'bar', }] }; option && zhudiv.setOption(option); zhudiv.on('click',function (params) { // 获取table下所有的tr let trs = $("# div table tbody tr"); for (let i = 0;i<trs.length;i++){ // 获取tr下所有的td let tds = trs.eq(i).find("td"); // 先把之前的标记的success去掉 $("#div table tbody tr").eq(i).removeClass('success'); // 如果点击图示的名字和table下的某一个行的第一个td的值一样 if (params.name == tds.eq(0).text()){ //设置success状态 $("#div table tbody tr").eq(i).addClass('success'); // 跳转到页面指定的id位置 $("html,body").animate({scrollTop:$("#div table tbody tr").eq(i).offset().top},1000); } } }); // 当鼠标落在tr时,显示浮动 $("#div table tbody").find("tr").on("mouseenter",function () { // 获得当前匹配元素的个数 let row = $(this).prevAll().length; // 获得当前tr下td的名字 let name = $("#div table tbody").find("tr").eq(row).find("td").eq(0).text(); // 设置浮动 zhudiv.dispatchAction({ type: 'showTip',seriesIndex: 0, name:name});//选中高亮 }); // 当鼠标移开tr时候取消浮动 $("#div table tbody").find("tr").on("mouseleave",function () { // 获得当前匹配元素的个数 let row = $(this).prevAll().length; // 获得当前tr下td的名字 let name = $("#div table tbody").find("tr").eq(row).find("td").eq(0).text(); // 设置浮动 zhudiv.dispatchAction({ type: 'hideTip', name:name});//选中高亮 }); }, error : function() { alert("请求失败"); }, }); </script> </body> </html>
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <style > body{ background: url(aifusen.jpg); background-size:100% 100% ; background-attachment: fixed ; } table{ border-spacing: 10px 20px; border : solid 1px red; margin-top:300px; } table tr{ width :100px; } </style> <body > <% Object message = request.getAttribute("message"); if(message!=null && !"".equals(message)){ %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); </script> <%} %> <table align="center" valign="middle"> <tr> <td align="center"> <a href="check.jsp">查询论文</a></td></tr> <tr> <td align="center"> <a href="add.jsp">增加论文</a></td></tr> </table> </form> </body> </html>
result.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <style > body{ background: url(aifusen.jpg); background-size:100% 100% ; background-attachment: fixed ; } table{ border-spacing: 10px 20px; border : solid 1px red; margin-top:300px; } table tr{ width :100px; } </style> </head> <body> <% Object message = request.getAttribute("message"); if(message!=null && !"".equals(message)){ %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); </script> <%} %> <p> 成功进入</p> <div id="result" align="center"> <c:forEach var="u" items="${list}"> <div> <table > <tr><td>id: </td> <td> ${u.id } </td></tr> <tr><td>关键词为: </td> <td> ${u.keywords } </td></tr> <tr><td>标题为: </td> <td> ${u.title } </td></tr> <tr><td>链接为: </td> <td> ${u.link } </td></tr> </table> </div> <div> <form action = "Servlet?method=delete" method="post" > <input type="hidden" name="delete" id ="delete" value ="${u.id}"> <input type="submit" value = "删除"> </form> <form action = "Servlet?method=alter" method="post" > <input type="hidden" name="alter" id ="later" value ="${u.id}"> <input type="submit" value = "修改"> </form> </div> </c:forEach> </div> </body> </html>

 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号