2024.5.6

第四十一天

所花时间:2小时

代码量:300+

博客量:1

了解到的知识点:放了个五一假期,虽然没有发博客,但是代码还是一直在敲的。发一下查询系统的代码

package Bean;

public class bean {
    private String id;
    private String name;//
    private String type;//
    private String category;
    private String range;
    private String document;
    private String organ;//
    private String viadata;
    private String pubdata;//
    private String perdata;
    Menu menu;


    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 String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getRange() {
        return range;
    }

    public void setRange(String range) {
        this.range = range;
    }

    public String getDocument() {
        return document;
    }

    public void setDocument(String document) {
        this.document = document;
    }

    public String getOrgan() {
        return organ;
    }

    public void setOrgan(String organ) {
        this.organ = organ;
    }

    public String getViadata() {
        return viadata;
    }

    public void setViadata(String viadata) {
        this.viadata = viadata;
    }

    public String getPubdata() {
        return pubdata;
    }

    public void setPubdata(String pubdata) {
        this.pubdata = pubdata;
    }

    public String getPerdata() {
        return perdata;
    }

    public void setPerdata(String perdata) {
        this.perdata = perdata;
    }
}
package Bean;
import java.util.List;

public class Menu {
    private Integer id;
    private String type;

    private List<Menu> bean;
    public Menu(Integer id,Integer pid,String type)
    {
        this.id =id;
        this.type= type;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }



    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }


    @Override
    public String toString() {
        return "Menu{" +
                "id=" + id +
                ", type='" + type + '\'' +
                ", bean=" + bean +
                '}';
    }
    public List<Menu> getBean(){
        return bean;
    }

    public void setBean(List<Menu> bean) {
        this.bean = bean;
    }
}
package Dao;

import Bean.bean;
import DBUtil.dbutil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class dao {

    public List<bean> searchAll( ){
        List<bean> list = new ArrayList<bean>();
        try {
            Connection conn = dbutil.getConn();
            Statement state = null;
            String sql="select * from tb_user.policy";
            PreparedStatement  pstmt = conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            System.out.println("搜索全部政策");
            while(rs.next()){
                bean lu = new bean();

                lu.setId(rs.getString("id"));
                lu.setType(rs.getString("type"));
                lu.setOrgan(rs.getString("organ"));
                lu.setPubdata(rs.getString("pubdata"));
                lu.setName(rs.getString("name"));
                list.add(lu);

            }
            rs.close();
            pstmt.close();
            conn.close();
        }catch(SQLException e) {

            System.out.println("发生错误");
            e.printStackTrace();


        }


        return list;

    }


    public List<bean> search(String name, String document, String organ, String fullText) {
        List<bean> list = new ArrayList<>();
        try {
            Connection conn = dbutil.getConn();
            StringBuilder sqlBuilder = new StringBuilder("SELECT * FROM tb_user.policy WHERE 1=1");

            // 根据提供的参数追加条件
            if (name != null && !name.isEmpty()) {
                sqlBuilder.append(" AND name LIKE ?");
            }
            if (document != null && !document.isEmpty()) {
                sqlBuilder.append(" AND document LIKE ?");
            }
            if (organ != null && !organ.isEmpty()) {
                sqlBuilder.append(" AND organ LIKE ?");
            }
            if (fullText != null && !fullText.isEmpty()) {
                sqlBuilder.append(" AND MATCH(`text`) AGAINST (? IN BOOLEAN MODE)");
            }

            PreparedStatement pstmt = conn.prepareStatement(sqlBuilder.toString());

            // 设置预处理语句的参数
            int parameterIndex = 1;
            if (name != null && !name.isEmpty()) {
                pstmt.setString(parameterIndex++, "%" + name + "%");
            }
            if (document != null && !document.isEmpty()) {
                pstmt.setString(parameterIndex++, "%" + document + "%");
            }
            if (organ != null && !organ.isEmpty()) {
                pstmt.setString(parameterIndex++, "%" + organ + "%");
            }
            if (fullText != null && !fullText.isEmpty()) {
                pstmt.setString(parameterIndex, fullText);
            }

            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                bean lu = new bean();
                // 设置 bean 属性
                lu.setId(rs.getString("id"));
                lu.setType(rs.getString("type"));
                lu.setOrgan(rs.getString("organ"));
                lu.setPubdata(rs.getString("pubdata"));
                lu.setName(rs.getString("name"));
                // 将 bean 对象添加到列表中
                list.add(lu);
            }
            rs.close();
            pstmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }



    public static bean getOne(String id){
        bean lu = new bean();
        try {
            Connection conn = dbutil.getConn();
            Statement state = null;
            String sql="select * from tb_user.policy where id=?";
            PreparedStatement  pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,id);
            ResultSet rs = pstmt.executeQuery();
            while(rs.next()){

                lu.setId(rs.getString("id"));
                lu.setType(rs.getString("type"));
                lu.setCategory(rs.getString("category"));
                lu.setOrgan(rs.getString("organ"));
                lu.setPubdata(rs.getString("pubdata"));
                lu.setName(rs.getString("name"));
                lu.setDocument(rs.getString("document"));
                lu.setRange(rs.getString("range"));
                lu.setPerdata(rs.getString("perdata"));
                lu.setViadata(rs.getString("viadata"));

            }
            rs.close();
            pstmt.close();
            conn.close();
        }
        catch(SQLException e) {

            e.printStackTrace();
        }
        return lu;

    }
    public String getDetailText(String id) {
        String detailText = null;
        try {
            Connection conn = dbutil.getConn();
            String sql = "SELECT text FROM tb_user.policy WHERE id=?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, id);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                detailText = rs.getString("text");
            }
            rs.close();
            pstmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return detailText;
    }



}
package DBUtil;

import java.sql.*;
public class dbutil {


    static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver"; //数据库驱动名
    static final String url = "jdbc:mysql://localhost:3306/tb_user";//数据库地址
    static final String user = "root";
    static final String password = "ADGJL12345syl";



    //连接数据库
    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 tb_user.policy limit 10";//数据库名称
        preparedStatement = conn.prepareStatement(sql);
        rs = preparedStatement.executeQuery();
        if(rs.next()){
            System.out.println("数据库不为空");
        }
        else{
            System.out.println("数据库为空");
        }
    }



}
<%--
  Created by IntelliJ IDEA.
  User: 郑天羽
  Date: 2024/4/30
  Time: 16:58
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="Dao.dao" %>
<%
    String id = request.getParameter("id");
    dao dao = new dao();
    String detailText = dao.getDetailText(id);
%>
<%= detailText %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <script th:src="@{/layuimini/js/lay-module/echarts/echarts.js}"></script>
  <script th:src="@{/layuimini/js/lay-module/echarts/wordcloud.js}"></script>
  <link rel="stylesheet" type="text/css" href="https://www.layuicdn.com/layui/css/layui.css" />
  <script src="https://www.layuicdn.com/layui/layui.js"></script>
</head>
<body>
<div style="width:100%; height:65px; float:left; position:relative; display:table-cell; vertical-align:middle; background-color:rgba(232,232,232,0.76);">
  <img src="LOGO.png" alt="Logo" style="height: 65px;">
  <span style="color: blue; font-weight: bold; font-size: 30px;">科技政策查询系统</span>
  <!-- 其他表单内容 -->
</div>


<form class="layui-form" action="table1.jsp" method="post" target="hideIframe1" >
  <p> </p>
  <div class="layui-form-item">

    <div class="layui-inline">

      <label class="layui-form-label">
        政策名称:
      </label>
      <div class="layui-input-inline">
        <input type="text" name="name"  class="layui-input">
      </div>
    </div>
    <div class="layui-inline">
      <label class="layui-form-label">
        政策文号:
      </label>
      <div class="layui-input-inline">
        <input type="text" name="document"  class="layui-input">
      </div>
    </div>
    <div class="layui-inline">
      <label class="layui-form-label">
        发文机构:
      </label>
      <div class="layui-input-inline">
        <input type="text" name="organ"  class="layui-input">
      </div>
      <label class="layui-form-label">
        全文检索:
      </label>
      <div class="layui-input-inline">
        <input type="text" name="fullText" class="layui-input">
      </div>
    </div>
    <div class="layui-inline">
      <div class="layui-input-inline">
        <button type="submit" class="layui-btn" lay-submit lay-filter="demo1"style="background-color: #1571b2">查询</button>
      </div>
    </div>
  </div>
</form>
</div>
<div class="container3">
  <iframe id="myIframe1" name="hideIframe1" style="" src="table1.jsp" frameborder="0" width="100%" height="850px"></iframe>
</div>
<!-- 分页组件 -->
<div class="layui-box layui-laypage layui-laypage-default" id="page">
  <!-- 这里是分页组件的内容 -->
</div>
<footer style="width: 100%; height: 50px; background-color: #f2f2f2; text-align: center; line-height: 50px;">
  Copyright 1996-2022 All Rights Reserved 版权所有:河北省科学技术情报研究院 河北省科技创新战略研究院 技术支持:河北省科技信息处理实验室
</footer>


<script>
  layui.use('laypage', function(){
    var laypage = layui.laypage;

    //执行一个laypage实例
    laypage.render({
      elem: 'page', //注意,这里的 test1 是 ID,不用加 # 号
      count: 1000, //数据总数,从服务端得到
      limit: 10, //每页显示的条数
      jump: function(obj, first){
        //首次不执行
        if(!first){
          //do something
        }
      }
    });
  });
</script>
</body>
</html>
<%@ page import="Dao.dao" %>
<%@ page import="Bean.bean" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <script th:src="@{/layuimini/js/lay-module/echarts/echarts.js}"></script>
  <script th:src="@{/layuimini/js/lay-module/echarts/wordcloud.js}"></script>
  <link rel="stylesheet" type="text/css" href="https://www.layuicdn.com/layui/css/layui.css" />
  <script src="https://www.layuicdn.com/layui/layui.js"></script>
    <meta charset="UTF-8">
    <title>Title</title>
    <!-- 引入 jQuery 库 -->
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <!-- 其他资源引入 -->
</head>
<body>

<jsp:useBean id="util" class="Dao.dao"/>
<%
  request.setCharacterEncoding("utf-8");
  response.setCharacterEncoding("utf-8");
  response.setContentType("text/html;charset=utf-8");
  String name=request.getParameter("name");
  String document=request.getParameter("document");
  String organ=request.getParameter("organ");
  String fulltext=request.getParameter("fulltext");
%>

<%
  // 判断是否执行多条件搜索
  if (name == null && document == null && organ == null&&fulltext==null){
    // 执行搜索全部
    List<bean> list = util.searchAll();
%>
<table class="layui-table" style="table-layout: fixed; word-wrap: break-word;">
  <!-- 表头 -->
  <thead>
  <tr>
    <td style="width: 300px">政策名称</td>
    <td style="width: 150px">发文机构</td>
    <td style="width: 70px">颁布日期</td>
    <td style="width: 80px">政策分类</td>
    <td style="width: 100px">操作</td>
  </tr>
  </thead>
  <!-- 表格内容 -->
  <tbody>
  <% for (bean b : list) { %>
  <tr>
    <td style="width: 300px"><%= b.getName() %></td>
    <td style="width: 150px"><%= b.getOrgan() %></td>
    <td style="width: 70px"><%= b.getPubdata() %></td>
    <td style="width: 80px"><%= b.getType() %></td>
    <td style="width: 100px">
      <button type="button" class="layui-btn layui-btn-sm" onclick="showDetail('<%= b.getId() %>')" style="background-color: #1571b2">查看详情</button>
    </td>
  </tr>
  <% } %>
  </tbody>
</table>
<% } else {
  // 执行多条件搜索
  List<bean> list = util.search(name, document, organ,fulltext);
%>
<div class="container3" id="container3">
  <table class="layui-table" style="table-layout: fixed; word-wrap: break-word;">
    <!-- 表头 -->
    <thead>
    <tr>
      <td style="width: 200px">政策名称</td>
      <td style="width: 150px">发文机构</td>
      <td style="width: 100px">颁布日期</td>
      <td style="width: 150px">政策分类</td>
      <td style="width: 100px">操作</td>
    </tr>
    </thead>
    <!-- 表格内容 -->
    <tbody>
    <% for (bean b : list) { %>
    <tr>
      <td style="width: 200px"><%= b.getName() %></td>
      <td style="width: 150px"><%= b.getOrgan() %></td>
      <td style="width: 100px"><%= b.getPubdata() %></td>
      <td style="width: 150px"><%= b.getType() %></td>
      <td style="width: 100px">
        <button type="button" class="layui-btn" onclick="showDetail('<%= b.getId() %>')" style="background-color: #1571b2">查看详情</button>
      </td>

    </tr>
    <% } %>
    <p><a href="javascript:history.go(-1);"><button class="layui-btn" style="background-color: #1571b2">返回</button></a></p>
    </tbody>
  </table>
</div>
<% } %>

<!-- 弹窗显示详情 -->
<div id="detailModal" class="layui-modal">
  <div class="layui-modal-content" style="padding: 20px;">
    <div id="detailContent"></div>
  </div>
</div>

<script>
  function showDetail(id) {
    $.ajax({
      type: "GET",
      url: "getDetail.jsp",
      data: { id: id },
      success: function(response) {
        // 只显示获取到的文本内容在弹窗中
        var detailText = $(response).text();
        $("#detailContent").text(detailText);
        layui.use('layer', function(){
          var layer = layui.layer;
          layer.open({
            type: 1,
            title: '详情',
            content: $('#detailModal'),
            area: ['600px', '400px'],
            end: function () {
              clearDetailContent();
            }
          });
        });
      },
      error: function(xhr, status, error) {
        console.error(xhr.responseText);
      }
    });
  }

  function clearDetailContent() {
    $("#detailContent").empty();
  }
</script>


</body>
</html>

 

posted @ 2024-05-26 23:44  cvjj  阅读(9)  评论(0)    收藏  举报