实现热词的查询删除
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8");%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <script src="js/jquery.min.js"></script> <script src="js/echart3.js"></script> <script src="js/echarts-wordcloud.js"></script> <title>Insert title here</title> <style> .myButton { box-shadow: 0px 0px 0px 2px #9fb4f2; background:linear-gradient(to bottom, #7892c2 5%, #476e9e 100%); background-color:#7892c2; border-radius:10px; border:1px solid #4e6096; display:inline-block; cursor:pointer; color:#ffffff; font-family:Arial; font-size:19px; padding:12px 37px; text-decoration:none; text-shadow:0px 1px 0px #283966; } .myButton:hover { background:linear-gradient(to bottom, #476e9e 5%, #7892c2 100%); background-color:#476e9e; } .myButton:active { position:relative; top:1px; } div{ text-align:center; display:block; } .a{ margin-top: 20px; } .b{ font-size: 20px; width: 160px; color: white; background-color: greenyellow; } #main{ width:100%; height: 400px; text-align:center; border:0px solid #ddd; float:center; } </style> </head> <body> <script type="text/javascript" src="./jquery-3.4.1.min.js"></script> <script type="text/javascript" src="./miaov.js"></script> <h2 style="color: red;text-align:center">论文模糊查询</h2> <div> </div> <form id="form1" name="form1" method="post" action="Servlet?method=cha"> <div>    标题: <input type="text" id="title" name="title"> <br> 关键词: <input type="text" id="keywords" name="keywords"><br><br> <input type="submit" class="myButton"value="查询" /> </div> </form> <br><br><br> <form actoin="" id="shuzi2"method="post"> 输入热词的数量查看热词云:<input type="text" name="shuzi"id="shuzi"><input type="button" onclick="cloud()"value="确定"> </form> <div id="main" align="center"> </div> <body background="images/bg.png" style=" background-repeat:no-repeat ;background-size:100% 100%; background-attachment: fixed;"> <script type="text/javascript"> function cloud(){ var dt; $.ajax({ url : "Servlet?method=cld&ran="+Math.random(), async : true, type : "POST", data :$('#shuzi2').serialize(), dataType : "json", success : function(cvpr) { dt = cvpr; 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); } var myChart = echarts3.init(document.getElementById('main')); //设置点击效果 myChart.setOption({ title: { text: '' }, tooltip: {}, series: [{ type : 'wordCloud', //类型为字符云 shape:'smooth', //平滑 gridSize : 8, //网格尺寸 size : ['50%','50%'], //sizeRange : [ 50, 100 ], rotationRange : [-45, 0, 45, 90], //旋转范围 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 : '#333' //阴影颜色 } }, left: 'center', top: 'center', right: null, bottom: null, '100%', height:'100%', data:mydata }] }); }, error : function() { alert($('#shuzi2').serialize()); alert("请求失败"); }, }); } </script> </body> </html>
cvpr
package cvpr; public class cvpr { private int id; public cvpr(int id, String title, String jianjie,String link, String keywords) { super(); this.id = id; this.link = link; this.keywords = keywords; this.title = title; this.jianjie = jianjie; } public int getId() { return id; } public void setId(int id) { this.id = id; } private String link; private String keywords; private String title; private String jianjie; private int value; public cvpr(String title,int value) { this.title=title; this.value=value; } public String getLink() { return link; } public void setLink(String link) { this.link = link; } public String getKeywords() { return keywords; } public void setKeywords(String keywords) { this.keywords = keywords; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getJianjie() { return jianjie; } public void setJianjie(String jianjie) { this.jianjie = jianjie; } public cvpr( String title, String jianjie,String link, String keywords) { super(); this.link = link; this.keywords = keywords; this.title = title; this.jianjie = jianjie; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } }
Dao
package Dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.Statement; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.stream.Collectors; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Map; import DBUtil.DBUtil; import cvpr.cvpr; public class Dao { public boolean add(cvpr c) { String sql = "insert into cvpr(title,abstract,link,keywords) values('"+ c.getTitle() + "','"+ c.getJianjie() + "','" + c.getLink() + "','" + c.getKeywords() + "')"; System.out.println(sql); Connection conn = DBUtil.getConn(); Statement state = null; boolean f = false; int a = 1; try { state = conn.createStatement(); state.executeUpdate(sql); } catch (Exception e) { e.printStackTrace(); a=0; } finally { DBUtil.close(state, conn); } if (a > 0) { f = true; } return f; } public boolean delete(int i) { String sql = "delete from cvpr where id='"+i+"'"; System.out.println(sql); Connection conn = DBUtil.getConn(); Statement state = null; boolean f = false; int a = 1; try { state = conn.createStatement(); state.executeUpdate(sql); } catch (Exception e) { e.printStackTrace(); a=0; } finally { DBUtil.close(state, conn); } if (a > 0) { f = true; } return f; } public List<cvpr> cha(String a,String b) { String sql="select * from cvpr where title like '%"+a+"%' and keywords like '%"+b+"%'"; System.out.println(sql); Connection conn = DBUtil.getConn(); Statement st = null; List<cvpr> c=new ArrayList<>(); ResultSet rs=null; try { st=conn.createStatement(); st.executeQuery(sql); rs=st.executeQuery(sql); while(rs.next()) { int id=rs.getInt("id"); String title = rs.getString("title"); String jianjie=rs.getString("abstract"); String link = rs.getString("link"); String keywords = rs.getString("keywords"); cvpr c2=new cvpr(id,title,jianjie,link,keywords); c.add(c2); } } catch (Exception e) { e.printStackTrace(); } finally { DBUtil.close(st, conn); } return c; } public static Map<String,Integer> getrc() { String sql="select * from cvpr"; Map<String, Integer>map= new HashMap<String, Integer>(); Map<String, Integer>results= new LinkedHashMap<String, Integer>(); Connection con=null; Statement state=null; ResultSet rs=null; con=DBUtil.getConn(); try { state=con.createStatement(); rs=state.executeQuery(sql); while(rs.next()) { String keywords=rs.getString("keywords"); //System.out.println(keywords); String[] split = keywords.split(","); for(int i=0;i<split.length;i++) { if(map.get(split[i])==null) { map.put(split[i],0); } else { map.replace(split[i], map.get(split[i])+1); } } } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } DBUtil.close(rs, state, con); map.entrySet() .stream() .sorted((p1, p2) -> p2.getValue().compareTo(p1.getValue())) .collect(Collectors.toList()) .forEach(ele -> results.put(ele.getKey(), ele.getValue())); return results; } public List<cvpr> list(String keywords) { // 查询所有信息 List<cvpr> list = new ArrayList<cvpr>(); // 创建集合 Connection conn = DBUtil.getConn(); String sql = "select * from cvpr where keywords like "+"'%"+keywords+"%'"; // SQL查询语句 try { PreparedStatement pst = conn.prepareStatement(sql); ResultSet rs = pst.executeQuery(); cvpr c = null; while (rs.next()) { String title = rs.getString("title"); String link = rs.getString("link"); String as= rs.getString("abstract"); c = new cvpr(title,link,as,keywords); list.add(c); } rs.close(); // 关闭 pst.close(); // 关闭 } catch (SQLException e1) { e1.printStackTrace(); // 抛出异常 } return list; // 返回一个集合 } public static List<cvpr> list2() { // 查询所有信息 List<cvpr> list2 = new ArrayList<cvpr>(); // 创建集合 Connection conn = DBUtil.getConn(); String sql = "select * from cvpr "; // SQL查询语句 try { PreparedStatement pst = conn.prepareStatement(sql); ResultSet rs = pst.executeQuery(); cvpr c = null; while (rs.next()) { String title = rs.getString("title"); String link = rs.getString("link"); String as= rs.getString("abstract"); String keywords=rs.getString("keywords"); c = new cvpr(title,link,as,keywords); list2.add(c); } rs.close(); // 关闭 pst.close(); // 关闭 } catch (SQLException e1) { e1.printStackTrace(); // 抛出异常 } return list2; // 返回一个集合 } }
DBUtil
package DBUtil; import java.sql.*; public class DBUtil { public static String db_url = "jdbc:mysql://localhost:3306/mysql?serverTimezone=GMT%2B8&useSSL=false"; public static String db_user = "root"; public static String db_pass = "chenhan"; public static Connection getConn () { Connection conn = null; try { Class.forName("com.mysql.cj.jdbc.Driver"); conn = DriverManager.getConnection(db_url, db_user,db_pass); } catch (Exception e) { e.printStackTrace(); } return conn; }//end getConn 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 pstmt = null; ResultSet rs = null; String sql ="select * from cvpr"; pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); if(rs.next()){ System.out.println("连接成功"); }else{ System.out.println("连接失败"); } } }
Servlet
package Servlet; import java.util.List; import java.util.Map; import java.io.IOException; 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 javax.servlet.http.HttpSession; import Dao.Dao; import cvpr.Tu; import cvpr.cvpr; import net.sf.json.JSONArray; import net.sf.json.JSONObject; @WebServlet("/Servlet") public class Servlet extends HttpServlet { private static final long serialVersionUID = 1L; public Servlet() { super(); } protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String method = req.getParameter("method"); System.out.println("me"+method); if ("add".equals(method)) { add(req, resp); } if ("cha".equals(method)) { cha(req, resp); } if ("delete".equals(method)) { delete(req, resp); } if("cld".equals(method)) { cld(req,resp); } } private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); HttpSession session = req.getSession(); String title = req.getParameter("title"); String jianjie = req.getParameter("jianjie"); String link = req.getParameter("link"); String keywords = req.getParameter("keywords"); /* session.setAttribute("tid",id); session.setAttribute("tname",name); session.setAttribute("tsex",sclass); session.setAttribute("tlocation",kecheng); session.setAttribute("tzc",score);*/ cvpr c = new cvpr(title,jianjie,link,keywords); Dao dao =new Dao(); boolean f=dao.add(c); System.out.println(f); if(f) { req.setAttribute("message", "添加成功!"); req.getRequestDispatcher("index.jsp").forward(req,resp); } else { req.setAttribute("message", "添加失败!"); req.getRequestDispatcher("index.jsp").forward(req,resp); } } private void cha(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); HttpSession session = req.getSession(); String title = req.getParameter("title"); String keywords = req.getParameter("keywords"); Dao dao =new Dao(); List<cvpr> c1=dao.cha(title,keywords); System.out.println(c1.get(0).getId()); req.setAttribute("c1", c1); req.setAttribute("title", title); req.setAttribute("keywords", keywords); req.getRequestDispatcher("cha.jsp").forward(req,resp); } private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); HttpSession session = req.getSession(); int id = Integer.parseInt(req.getParameter("id")); System.out.println(id); Dao dao =new Dao(); boolean f=dao.delete(id); if(f) { req.setAttribute("message", "删除成功!"); req.getRequestDispatcher("index.jsp").forward(req,resp); } else { req.setAttribute("message", "添加失败!"); req.getRequestDispatcher("index.jsp").forward(req,resp); } System.out.println(f); } private void cld(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ System.out.println("Rcservlet 运行"); resp.setContentType("text/html;charset=UTF-8"); req.setCharacterEncoding("UTF-8"); int shuzi=Integer.parseInt(req.getParameter("shuzi")); System.out.println("123"); Map<String, Integer>sortMap=Dao.getrc(); JSONArray json =new JSONArray(); int k=0; for (Map.Entry<String, Integer> entry : sortMap.entrySet()) { JSONObject ob=new JSONObject(); ob.put("name", entry.getKey()); ob.put("value", entry.getValue()); if(!(entry.getKey().equals("for")||entry.getKey().equals("and")||entry.getKey().equals("With")||entry.getKey().equals("of")||entry.getKey().equals("in")||entry.getKey().equals("From")||entry.getKey().equals("A")||entry.getKey().equals("to")||entry.getKey().equals("a")||entry.getKey().equals("the")||entry.getKey().equals("by"))) { json.add(ob); k++; } if(k==shuzi) break; } System.out.println(json.toString());//输出JSON数据 resp.getWriter().write(json.toString()); } }