Policy:
package bean; public class Policy { private String name;//政策名称 private String organ;//发文机构 private String date; private String type; public Policy(String name, String organ, String date, String type) { this.name = name; this.organ = organ; this.date = date; this.type = type; } @Override public String toString() { return "Policy{" + "name='" + name + '\'' + ", organ='" + organ + '\'' + ", date='" + date + '\'' + ", type='" + type + '\'' + '}'; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getOrgan() { return organ; } public void setOrgan(String organ) { this.organ = organ; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public String getType() { return type; } public void setType(String type) { this.type = type; } }
shu:
package bean; import java.util.List; public class shu { private String id; private String name; private int num; private List<shu> son; public shu() { } @Override public String toString() { return "shu{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", num=" + num + ", son=" + son + '}'; } public shu(String id, String name, List<shu> son) { this.id = id; this.name = name; this.son = son; } public shu(String id, String name) { this.id = id; this.name = name; } public shu(String id, String name, int num, List<shu> son) { this.id = id; this.name = name; this.num = num; this.son = son; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } public List<shu> getSon() { return son; } public void setSon(List<shu> son) { this.son = son; } }
Text:
package bean; public class Text { private String text; public Text(String text) { this.text = text; } public String getText() { return text; } public void setText(String text) { this.text = text; } }
dao:
package dao; import java.sql.Connection; import java.util.ArrayList; import java.util.List; import bean.*; import java.sql.ResultSet; import java.sql.Statement; import dbutil.DBUtil; public class dao { public static List<Policy> SelectAll(String name,String doucument,String organ,String full) { System.out.println("11111111111"); String sql = "select * from policy where "; if (!name.equals("")){ sql+="name like '%"+name+"%'"; } if (!doucument.equals("")){ if(!name.equals("")) sql+="and "; sql+="document like'%"+doucument+"%'"; } if (!organ.equals("")){ if (!name.equals("")||!doucument.equals("")) sql+="and "; sql+="organ like '%"+organ+"%'"; } Connection conn = DBUtil.getConn();//这里就是从DBUtil类里面得到连接 Statement state =null; List<Policy> list = new ArrayList<>(); ResultSet rs = null; int flag=0; try { System.out.println("22222222222222"); state = conn.createStatement(); rs=state.executeQuery(sql); Policy kc=null; while(rs.next()) { String name1=rs.getString("name"); String organ1=rs.getString("organ"); String date=rs.getString("perdata"); String type=rs.getString("type"); kc =new Policy(name1,organ1,date,type); System.out.println(kc.toString()); list.add(kc); } // System.out.println("3"); } catch(Exception e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return list; } public static List<Policy> SelectType(String method) { String sql = "select * from policy where type like '"+method+"'"; Connection conn = DBUtil.getConn();//这里就是从DBUtil类里面得到连接 Statement state =null; List<Policy> list = new ArrayList<>(); ResultSet rs = null; int flag=0; try { state = conn.createStatement(); rs=state.executeQuery(sql); Policy kc=null; while(rs.next()) { String name1=rs.getString("name"); String organ1=rs.getString("organ"); String date=rs.getString("perdata"); String type=rs.getString("type"); kc =new Policy(name1,organ1,date,type); // System.out.println(kc.toString()); list.add(kc); } // System.out.println("3"); } catch(Exception e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return list; } public static List<Text> Select(String method) { String sql = "select * from policy where name = '"+method+"'"; Connection conn = DBUtil.getConn();//这里就是从DBUtil类里面得到连接 Statement state =null; List<Text> list = new ArrayList<>(); ResultSet rs = null; int flag=0; try { state = conn.createStatement(); rs=state.executeQuery(sql); Text kc=null; while(rs.next()) { String text=rs.getString("text"); kc =new Text(text); // System.out.println(kc.toString()); list.add(kc); } // System.out.println("3"); } catch(Exception e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return list; } public static List<shu> shu() { String sql = "select * from policy_class"; Connection conn = DBUtil.getConn();//这里就是从DBUtil类里面得到连接 Statement state =null; List<shu> list = new ArrayList<>(); ResultSet rs = null; ResultSet rs1 = null; int flag=0; try { state = conn.createStatement(); rs=state.executeQuery(sql); shu kc=null; String www="01"; int i=0; List<shu> shuList=new ArrayList<>(); int j=0; while(rs.next()) { String id=rs.getString("Type_id"); String name=rs.getString("Type_Name"); String str=id.substring(0,2); // System.out.println(str); // System.out.println(www+"wwwwwwwwwwwww"); if (str.equals(www)&&!www.equals("01")){ shu kc1=new shu(id,name); kc1.setNum(num(name)); // System.out.println(kc1+" kc1"); shuList.add(kc1); j++; // System.out.println(kc1+" kc1"); continue; } www=str; if (j!=0){ try { list.get(i-1).setSon(shuList); }catch (ArrayIndexOutOfBoundsException e){ System.out.println("只 能输 "); } j=0; shuList=new ArrayList<>(); } kc =new shu(id,name); kc.setNum(num(name)); System.out.println(num(name)); // System.out.println(kc.toString()); list.add(kc); i++; } // System.out.println("3"); } catch(Exception e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return list; } public static int num(String method) { String sql = "select * from policy where type = '"+method+"'"; Connection conn = DBUtil.getConn();//这里就是从DBUtil类里面得到连接 Statement state =null; List<Text> list = new ArrayList<>(); ResultSet rs = null; int flag=0; try { flag=0; state = conn.createStatement(); rs=state.executeQuery(sql); while(rs.next()) { // System.out.println("我进来了"); flag++; } // System.out.println("3"); } catch(Exception e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return flag; } }
DBUtil:
package dbutil; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DBUtil {//这个类就是套模板如果需要用的时候只需要改数据库(db)的名字即可******* //public static String db_url="jdbc:mysql://localhost:3306/database?uerUnicode=true&characterEncoding=UTF-8"; public static String db_url="jdbc:mysql://localhost:3306/canteen?serverTimezone=GMT%2B8&serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8";//db是数据库的名字 //这是连接数据库,servlet是数据库的名称,uerUnicode=true&characterEncoding=UTF-8是将字符集设置为utf-8,避免乱码。 public static String db_user="root";//数据的用户名 public static String db_password="123456";//数据库的密码 public static Connection getConn()//获取连接,返回Connection类型,必须设置为static这样才能在其他类中使用 { Connection conn=null; try { Class.forName("com.mysql.cj.jdbc.Driver");//加载驱动 conn=DriverManager.getConnection(db_url,db_user,db_password);//连接数据库 } catch(Exception e) { e.printStackTrace(); } return 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(); } } } }
ceshi:
package servlet; import bean.shu; import dao.dao; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.annotation.*; import java.io.IOException; import java.util.List; @WebServlet("/ceshi") public class ceshi extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { List<shu> kc1= dao.shu(); for (shu shu : kc1) { if (shu.getSon()!=null){ int num=0; for (bean.shu shu1 : shu.getSon()) { num+=shu1.getNum(); } num+=shu.getNum(); shu.setNum(num); System.out.println("-----------------------------------"); } } request.setAttribute("kc1", kc1); request.getRequestDispatcher("/main.jsp").forward(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } }
Servlet:
package servlet; import bean.Policy; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.annotation.*; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.util.List; import java.util.Map; import bean.shu; import dao.*; @WebServlet("/servlet") public class Servlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException { return; } protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8");// 设置字符集,避免乱码 // 获取jsp界面需要进行的操作, String method = req.getParameter("method"); if (method.equals("SelectAll"))// 转到login函数 { SelectAll(req,resp); } else if (method.equals("shu")) { shu(req,resp); } else { SelectType(req,resp,method); } } private void SelectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); resp.setContentType("text/html;charset=utf-8"); String name=req.getParameter("name"); String document=req.getParameter("document"); String organ=req.getParameter("organ"); String full=req.getParameter("full"); List<Policy> kc1=dao.SelectAll(name,document,organ,full); req.setAttribute("kc", kc1); req.getRequestDispatcher("/main.jsp").forward(req, resp); } private void SelectType(HttpServletRequest req, HttpServletResponse resp,String method) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); resp.setContentType("text/html;charset=utf-8"); List<Policy> kc1=dao.SelectType(method); List<shu> kc2=dao.shu(); for (shu shu : kc2) { if (shu.getSon()!=null){ int num=0; for (bean.shu shu1 : shu.getSon()) { num+=shu1.getNum(); } num+=shu.getNum(); shu.setNum(num); System.out.println("-----------------------------------"); } } req.setAttribute("kc", kc1); req.setAttribute("kc1",kc2); // System.out.println(req.getAttribute("kc")+"qqqqqqqqqq"); req.getRequestDispatcher("/main.jsp").forward(req, resp); } private void shu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); resp.setContentType("text/html;charset=utf-8"); List<shu> kc1=dao.shu(); req.setAttribute("kc", kc1); req.getRequestDispatcher("/main.jsp").forward(req, resp); } }
Servlet1:
package servlet; import bean.Policy; import bean.Text; import dao.dao; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.annotation.*; import java.io.IOException; import java.util.List; @WebServlet("/servlet1") public class Servlet1 extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException { service(request,response); } protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8");// 设置字符集,避免乱码 // 获取jsp界面需要进行的操作, System.out.println("------------------------------------------------------"); String method = req.getParameter("method1"); System.out.println(method); resp.setContentType("text/html;charset=utf-8"); System.out.println(method); List<Text> kc1=dao.Select(method); req.setAttribute("kc", kc1); // System.out.println(req.getAttribute("kc")+"qqqqqqqqqq"); req.getRequestDispatcher("/vewe.jsp").forward(req, resp); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } }
ceshi.jsp:
<%-- Created by IntelliJ IDEA. User: LLL Date: 2022/10/26 Time: 13:24 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <a href="/untitled_war_exploded/ceshi">点击链接</a> </body> </html>
main.jsp
<%-- Created by IntelliJ IDEA. User: LLL Date: 2022/10/25 Time: 14:28 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@page isELIgnored="false" %> <html> <head> <title>科技政策查询系统</title> <script src="jquery-3.6.1-min.js"></script> </head> <link rel="stylesheet" href="layui/css/layui.css" media="all"> <script src="layui/layui.js" charset="utf-8"></script> <%--<script> function fun(){ $.ajax({ url:"servlet?method=SelectAll", type:"Get", data:{"name":$("#name").val(),"document":$("#document").val(),"organ":$("#organ").val(),"full":$("#full").val()}, success:function (data) { $("#name").val(""); $("#document").val(""); $("#organ").val(""); $("#full").val(""); }, error:function (){ alert("出错了") }, successful: function (data) { }, dataType:"text" }); } </script>--%> <body> <div class="layui-layout layui-layout-admin"> <div class="layui-header" style="background-color: whitesmoke"> <div class="layui-logo layui-hide-xs layui-bg-black">科技政策查询系统</div> <!-- 头部区域(可配合layui 已有的水平导航) --> <ul class="layui-nav layui-layout-left"> <!-- 移动端显示 --> <li class="layui-nav-item layui-show-xs-inline-block layui-hide-md" lay-header-event="menuLeft"> <i class="layui-icon layui-icon-spread-left"></i> </li> <li class=" layui-hide-xs" style="color:deepskyblue ;vertical-align: center"> <img src="layui/LOGO.png" alt="" width="45" height="45" style="margin-bottom:4px"> <span style="padding-top: 10px;font-size: 35px">科技政策查询系统</span> </li> </ul> <ul class="layui-nav layui-layout-right"> <li class="layui-nav-item layui-hide layui-show-md-inline-block"> <a href="javascript:;"> <img src="//tva1.sinaimg.cn/crop.0.0.118.118.180/5db11ff4gw1e77d3nqrv8j203b03cweg.jpg" class="layui-nav-img"> <span style="color: black">tester</span> </a> </li> </ul> </div> <div class="layui-side layui-bg-black"> <div class="layui-side-scroll"> <!-- 左侧导航区域(可配合layui已有的垂直导航) --> <ul class="layui-nav layui-nav-tree" lay-filter="test"> <li class="layui-nav-item"> <a href="javascript:;">科技文档管理</a> <dl class="layui-nav-child"> <dd><a href="javascript:;">list 1</a></dd> <dd><a href="javascript:;">list 2</a></dd> <dd><a href="">超链接</a></dd> </dl> </li> <li class="layui-nav-item"> <a href="javascript:;">系统设置</a> <dl class="layui-nav-child"> <dd><a href="javascript:;">list 1</a></dd> <dd><a href="javascript:;">list 2</a></dd> <dd><a href="">超链接</a></dd> </dl> </li> </ul> </div> </div> <div class="layui-body"> <!-- 内容主体区域 --> <div style="width: 200px;height: 815px;float: left"> <div class="layui-panel"> <ul class="layui-menu" id="docDemoMenu1"> <c:forEach items="${kc1}" var="shu"> <c:if test="${empty shu.son}"> <li lay-options="{id: 100}"> <div class="layui-menu-body-title"><a href="/untitled_war_exploded/servlet?method=${shu.name}">${shu.name}(${shu.num})</a></div> </li> </c:if> <c:if test="${not empty shu.son}" > <li class="layui-menu-item-parent" lay-options="{type: 'parent'}"> <div class="layui-menu-body-title"> <a href="/untitled_war_exploded/servlet?method=${shu.name}"> ${shu.name}(${shu.num})</a> <i class="layui-icon layui-icon-right"></i> </div> <div class="layui-panel layui-menu-body-panel"> <ul> <c:forEach items="${shu.son}" var="shu1"> <li lay-options="{id: 1051}"> <div class="layui-menu-body-title"> <a href="/untitled_war_exploded/servlet?method=${shu1.name}"> ${shu1.name}(${shu1.num})</a> </div> </li> </c:forEach> </ul> </div> </li> </c:if> </c:forEach> </ul> </div> </div> <div style="width: 1300px;height: 75px;float: top;background-color: #D9D9D9;"> <form action="/untitled_war_exploded/servlet?method=SelectAll" method="post"> <span style="margin-top: 50px"> 项目名称 </span> <input type="text" id="name" name="name" style="margin-top: 25px;height: 25px;width: 150px;border: none" placeholder="请输入政策名称"> <span style="margin-top: 50px">发文字号 </span> <input type="text" id="document" name="document" style="margin-top: 25px;height: 25px;width: 150px;border: none" placeholder="发文字号"> <span style="margin-top: 50px">发文机构 </span> <input type="text" id="organ" name="organ" style="margin-top: 25px;height: 25px;width: 150px;border: none" > <span style="margin-top: 50px">全文检索 </span> <input type="text" id="full" name="full" style="margin-top: 25px;height: 25px;width: 150px;border: none" > <input type="submit" class="layui-btn" > </form> </div> <div style="float:top;float: right"> <%-- <iframe src="select.jsp" id="main" height="100%" width="1110px"> </iframe>--%> <div style="float:top;float: right;width: 1110px;height: 100%"> <table class="layui-table" layui-data= "{skin: even:true, } "> <tr> <th>政策名称</th> <th>发文机构</th> <th>颁布日期</th> <th>政策分类</th> <th>操作</th> </tr> <c:forEach items="${kc}" var="Policy"> <tr> <td><c:out value="${Policy.name}"></c:out> </td> <td><c:out value="${Policy.organ}"></c:out></td> <td><c:out value="${Policy.date}"></c:out></td> <td><c:out value="${Policy.type}"></c:out></td> <td><a href="/untitled_war_exploded/servlet1?method1=${Policy.name}"}><button class="layui-btn">查看</button></a></td> </tr> </c:forEach> </table> </div> </div> </div> <%--<script> <c:forEach items="${kc}" var="Text"> if (${Text.text}!=null){ } alert() </c:forEach> </script>--%> <div class="layui-footer layui-bg-black"> <!-- 底部固定区域 --> <div style="text-align: center">Copyright 1996-2022 All Rights Reserved 版权所有:河北省科学技术情报研究院 河北省科技创新战略研究院 技术支持:河北省科技信息处理实验室。</div> <%-- 底部固定区域--%> </div> </div> <script src="./layui/layui.js"></script> <script> //JS layui.use(['element', 'layer', 'util'], function(){ var element = layui.element ,layer = layui.layer ,util = layui.util ,$ = layui.$; //头部事件 util.event('lay-header-event', { //左侧菜单事件 menuLeft: function(othis){ layer.msg('展开左侧菜单的操作', {icon: 0}); } ,menuRight: function(){ layer.open({ type: 1 ,content: '<div style="padding: 15px;">处理右侧面板的操作</div>' ,area: ['260px', '100%'] ,offset: 'rt' //右上角 ,anim: 5 ,shadeClose: true }); } }); }); </script> </body> </html>
vewe.jsp:
<%-- Created by IntelliJ IDEA. User: LLL Date: 2022/10/26 Time: 13:34 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@page isELIgnored="false" %> <html> <head> <title>Title</title> </head> <body> <c:forEach items="${kc}" var="Text"> ${Text.text} </c:forEach> </body> </html>
标签:name,shu,查询,科技,树形,nbsp,import,public,String From: https://www.cnblogs.com/1774323810com/p/16856507.html