• 10.29日进度报告


    做了一个查询界面,吸收了杨子光老师给的教训,空按按钮,查询显示所有内容

    package test2;
    
    import java.io.IOException;
    import java.util.ArrayList;
    
    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 test2.table;
    import test2.DBUtil;
    
    /**
     * Servlet implementation class queryTable
     */
    @WebServlet("/queryTable")
    public class queryTable extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public queryTable() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            request.setCharacterEncoding("utf-8");
            String type=request.getParameter("type");
            if(type.equals("all"))
            {
                ArrayList<table> list=DBUtil.gettables("all");
                request.setAttribute("list", list);
                request.getRequestDispatcher("chaxun.jsp").forward(request, response);
            }
            else
            {
                String value=request.getParameter("value");
                System.out.println(value);
                if(value.length()!=0) {
                    ArrayList<table> list=DBUtil.getDocesByCase(type,value);
                    request.setAttribute("list", list);
                    request.getRequestDispatcher("chaxun.jsp").forward(request, response);
                }
                else {
                    ArrayList<table> list=DBUtil.gettables("all");
                    request.setAttribute("list", list);
                    request.getRequestDispatcher("chaxun.jsp").forward(request, response);
                }
            }
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            doGet(request, response);
        }
    
    }
    package test2;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
    
    import org.apache.catalina.User;
    
    
    
    import test2.table;
    
    
    
    public class DBUtil {
        //数据库URL和账号密码
        private static final String connectionURL="jdbc:mysql://127.0.0.1:3306/doc_system?useUnicode=true&characterEncoding=GB18030&useSSL=false&serverTimezone=GMT&allowPublicKeyRetrieval=true";
        private static final String username="root";
        private static final String password="123456";
        
        //数据库连接
        public static Connection getConnection()
        {
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
                return DriverManager.getConnection(connectionURL,username,password);
            } catch (Exception e) {
                // TODO: handle exception
                System.out.println("数据库连接失败");
                e.printStackTrace();
                return null;
            }
        }
        public static void closeAll(Connection connection,PreparedStatement statement,ResultSet rSet)
        {
            try {
                if(connection!=null)
                    connection.close();
            } catch (SQLException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
        
            try {
                if(statement!=null)
                    statement.close();
            } catch (SQLException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
        
            try {
                if(rSet!=null)
                    rSet.close();
            } catch (SQLException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
        }
            
        //关闭connection和preparedstatement
        public static void closePart(Connection connection,PreparedStatement statement)
        {
            try {
                if(connection!=null)
                    connection.close();
            } catch (SQLException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
        
            try {
                if(statement!=null)
                    statement.close();
            } catch (SQLException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
        }
            
        public static void main(String[] args) {
            //getConnection();
            
        }
        public static ArrayList<table> gettables(String type) {
            Connection connection=null;
            PreparedStatement preparedStatement=null;
            ResultSet rSet=null;
            try {
                connection=getConnection();
                String sql;
                System.out.println(type);
                sql="select * from t_table";
                preparedStatement=connection.prepareStatement(sql);
                rSet=preparedStatement.executeQuery();
                ArrayList<table> list=new ArrayList<>();
                while(rSet.next())
                {
                    table doc=new table();
                    doc.setId(rSet.getInt("id"));
                    doc.setJgmc(rSet.getString("jgmc"));
                    doc.setXssh(rSet.getString("xssh"));
                    doc.setBmsh(rSet.getString("bmsh"));
                    doc.setGlcs(rSet.getString("glcs"));
                    doc.setResult(rSet.getInt("result"));
                    list.add(doc);
                }
                return list;
            } catch (SQLException e) {
                // TODO: handle exception
                e.printStackTrace();
            }
            finally {
                closeAll(connection, preparedStatement, rSet);
            }
            return null;
        }
        public static ArrayList<table> getDocesByCase(String type, String value) {
            Connection connection=null;
            PreparedStatement preparedStatement=null;
            ResultSet rSet=null;
            try {
                connection=getConnection();
                String sql="";
                System.out.println(type);
                if(type.equals("jgmc"))
                {
                    sql="select * from t_table where jgmc='"+value+"'";
                }
                else if(type.equals("sfsh"))
                {
                    sql="select * from t_table where sfsh='"+value+"'";
                }
                else if(type.equals("bmsfsh"))
                {
                    sql="select * from t_table where bmsfsh='"+value+"'";
                }
                else if(type.equals("result"))
                {
                    sql="select * from t_table where result='"+value+"'";
                }
                preparedStatement=connection.prepareStatement(sql);
                rSet=preparedStatement.executeQuery();
                ArrayList<table> list=new ArrayList<>();
                while(rSet.next())
                {
                    table doc=new table();
                    doc.setId(rSet.getInt("id"));
                    doc.setJgmc(rSet.getString("jgmc"));
                    doc.setXssh(rSet.getString("xssh"));
                    doc.setBmsh(rSet.getString("bmsh"));
                    doc.setGlcs(rSet.getString("glcs"));
                    doc.setResult(rSet.getInt("result"));
                    list.add(doc);
                }
                return list;
            } catch (SQLException e) {
                // TODO: handle exception
                e.printStackTrace();
            }
            finally {
                closeAll(connection, preparedStatement, rSet);
            }
            return null;
        }
    }
    <%@page import="test2.DBUtil"%>
    <%@page import="test2.table"%>
    <%@page import="java.util.ArrayList"%>
    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <!DOCTYPE html>
    <html>
    <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
     <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
     <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
     <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>
     <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
    <title>Insert title here</title>
    <style>
    .form-group{
    35%;
    float:left;
    margin-left:10px;
    margin-top:10px;
    }
    label{
    30%;
    float:left;
    margin-right:5px;
    margin-top:5px;
    }
    .form-control{
    60%;
    }
    .btn{
    100px;
    height:40px;
    border:0px;
    border-radius:5px;
    background-color:orange;
    color:black;
    margin-left:20px;
    margin-top:10px
    }
    </style>
    </head>
    <body>
        <div>
            <div class="form-group">
                <label for="type">请选择查询类型:</label>
                <select class="form-control" id="type">
                    <option value="jgmc">机构全称</option>
                    <option value="sfsh">形式审核</option>
                    <option value="bmsfsh">部门审核</option>
                </select>
            </div>
            <div class="form-group">
                <label for="content">请输入查询内容:</label>
                <input type="text" class="form-control" id="content">
            </div>
            <div>
                <input type="button" class="btn" value="查询" onclick="query()">
            </div>
        </div>
        <div>
            <table class="table table-hover table-striped table-bordered table-sm">
                <tr>
                    <th>编号</th>
                    <th>机构全称</th>
                    <th>形式审核</th>
                    <th>部门审核</th>
                    <th>管理处室</th>
                </tr>
                <%ArrayList<table> list=(ArrayList<table>)request.getAttribute("list"); %>
                <c:forEach var="l" items="<%=list %>" varStatus="i">
                    <tr>
                        <td id="id${i.index+1 }">${l.getId() }</td>
                        <td>${l.getJgmc() }</td>
                        <td>${l.getXssh() }</td>
                        <td>${l.getBmsh() }</td>
                        <td>${l.getGlcs() }</td>
                    </tr>
                </c:forEach>
            </table>
        </div>
    </body>
    <script>
        function query(){
            var sel=document.getElementById("type");
            var type=sel.options[sel.selectedIndex].value;
            var value=$("#content").val();
            window.location="queryTable?type="+type+"&value="+value;
        }
    </script>
    </html>
  • 相关阅读:
    apache设置无缓存
    唤醒键盘时取消对特定类的position:fixed定位
    vscode增加sftp扩展
    浮层蒙版加载中实现方式
    管理应用程序版本Elastic Beanstalk
    向 Elastic Beanstalk 环境中添加数据库
    在 Amazon ECS 上运行 X-Ray 守护程序
    SNS Message Attributes
    ProvisionedThroughputExceededException
    使用 Amazon CloudWatch Events 检测和响应管道状态更改
  • 原文地址:https://www.cnblogs.com/sunhongbin/p/14017479.html
Copyright © 2020-2023  润新知