行程管理系统

作者:@kuaiquxie
作者的github:https://github.com/bitebita
本文为作者原创,如需转载,请注明出处:https://www.cnblogs.com/dzwj/p/15579615.html


 

Javaweb

 

 

 Dao层:

复制代码
//连接数据库,实现增查功能
package dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import model.Model;

public class Dao {

        //URL是用来表示互联网上的某个资源地址,互联网上的每个文件都有一个唯一的 URL,它包含的信息指出文件的位置以及浏览器应该怎么处理它
        private  String URL= "jdbc:mysql1://localhost:3306/mysql?&useSSL=false&serverTimezone=UTC";
        //用户名
        private  String Name = "root";
        //用户密码
        private  String Password = "123456";

        //检测是否存在
        public boolean ifExit(String name) {
            if(Find(name)) {
                return true;
            }else  {
                return false;
            }
        }

        //连接数据库并实现增加功能
        public boolean add(Model claz ) {
             
            PreparedStatement ps = null;
            Connection con = null;
//            Model m = null;

            try {
    //驱动数据库
        Class.forName("com.mysql.cj.jdbc.Driver");
        System.out.println("驱动成功");
    //连接数据库
        con = DriverManager.getConnection(URL, Name, Password);
        System.out.println("连接成功");  
    //准备sql语句
        String sql = "insert into claz values (?,?,?,?,?,?,?,?)";
    //创建语句传输对象
                ps = con.prepareStatement(sql);
                ps.setString(1, claz .getName());
                ps.setInt(2, claz .getID());
                ps.setString(3, claz .getStudentType());
                ps.setString(4, claz .getCollage());
                ps.setString(5, claz .getPhonenum());
                ps.setString(6, claz .getColor());
                ps.setString(7, claz .getXingcheng());
                ps.setString(8, claz .getOther());
                int rs = ps.executeUpdate();

                if (rs > 0) {
                    return true;
                }else {
                    return false;
                }

            }catch (ClassNotFoundException e) {
                e.printStackTrace();
                return  false;
            }catch (SQLException e) {
                e.printStackTrace();
                return  false;
            }catch (Exception e) {
                e.printStackTrace();
                return  false;

            }finally {

                try {
                    if(ps!= null) ps.close();
                    if(con!= null) con.close();

                } catch (SQLException e) {
                    e.printStackTrace();
                }

            }


        }
  //实现查找功能
    public boolean Find(String name)  {

        PreparedStatement ps = null;
        Connection con = null;
        Model m = null;
        ResultSet rs = null;

        try {

            Class.forName("com.mysql.cj.jdbc.Driver");
            con = DriverManager.getConnection(URL,Name,Password);
            //准备sql语句
            String sql = "select * from claz where name =?";
            ps = con.prepareStatement(sql);
            ps.setString(1, name);
            rs = ps.executeQuery();

            if (rs.next()) {

                String name1 = rs.getString("name");
                int ID = rs.getInt("ID");
                String studentType = rs.getString("studentType");
                String collage = rs.getString("collage");
                String phonenum = rs.getString("phonenum");
                String color = rs.getString("color");
                String xingcheng = rs.getString("xingcheng");
                String other = rs.getString("other");

                Model m2 = new Model(name1, ID, studentType, collage, phonenum, color, xingcheng, other);
                m = m2;

                return true;

            }else {

                return false;

            }

        }catch (ClassNotFoundException e) {
            e.printStackTrace();
            return  false;
        }catch (SQLException e) {
            e.printStackTrace();
            return  false;
        }catch (Exception e) {

            e.printStackTrace();
            return  false;

        }finally {

            try {
                if(rs!= null) rs.close();
                if(ps!= null) ps.close();
                if(con!= null) con.close();

            } catch (SQLException e) {
                e.printStackTrace();
            }

        }

    }
    

}
复制代码

Model层:

复制代码
package model;

public class Model {
    private String name;
    private int ID;
    private String studentType;
    private String collage;
    private String  phonenum;
    private String color;
    private String xingcheng;
    private String other;

    public Model()
    {
        
    }
    
    public Model(String name, int ID, String studentType, String collage, String phonenum, String color, String xingcheng, String other)
    {
        this.name = name;
        this.ID = ID;
        this.studentType = studentType;
        this.collage = collage;
        this.phonenum = phonenum;
        this.color = color;
        this.xingcheng = xingcheng;
        this.other = other;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getID() {
        return ID;
    }

    public void setID(int ID) {
        this.ID = ID;
    }

    public String getStudentType() {
        return studentType;
    }

    public void setStudentType(String studentType) {
        this.studentType = studentType;
    }

    public String getCollage() {
        return collage;
    }

    public void setCollage(String collage) {
        this.collage = collage;
    }

    public String getPhonenum() {
        return phonenum;
    }

    public void setPhonenum(String phonenum) {
        this.phonenum = phonenum;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getXingcheng() {
        return xingcheng;
    }

    public void setXingcheng(String xingcheng) {
        this.xingcheng = xingcheng;
    }

    public String getOther() {
        return other;
    }

    public void setOther(String other) {
        this.other = other;
    }
    }
复制代码

claz_Servlet层:

复制代码
package service;

import model.Model;
import dao.Dao;

public class claz_Service {
    public static boolean add(Model claz) {
        
        Dao d = new Dao();

        if(d.Find(claz.getName())) {
            
            return  false;
        }else{
            
            d.add(claz);
            return  true;
        }

    }

}
复制代码

Servlet层:

复制代码
package servlet;

import model.Model;
import service.claz_Service;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class Servlet extends HttpServlet {
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {

    }

    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {

        String name = request.getParameter("name");
        int ID  =Integer.parseInt( request.getParameter("ID"));
        String studentType = (request.getParameter("studentType"));
        String collage = request.getParameter("collage");
        String phonenum = request.getParameter("phonenum");
        String color = request.getParameter("color");
        String[] xingcheng = request.getParameterValues("xingcheng");
        String other = request.getParameter("other");
        StringBuffer xingcheng2 = new StringBuffer();
        for(String i:xingcheng) {
            xingcheng2.append(i);
        }

        Model m = new Model(name, ID, studentType, collage, phonenum, color, xingcheng2.toString(), other);


        claz_Service cs = new claz_Service();
        boolean judge = cs.add(m);
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out =  response.getWriter();
        if(judge) {

            //out.println(cs.getName() + cs.getID());
            out.println("增加成功");

        }else  {

            //out.println(judge);
            request.getRequestDispatcher("index.jsp").forward(request, response);

        }

    }
}
复制代码

welcome.jsp文件:

复制代码
<%--
  Created by IntelliJ IDEA.
  User: Tufuir
  Date: 2021/11/19
  Time: 13:37
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>欢迎使用</title>
</head>
<body>

</body>
</html>
复制代码

login.jsp文件:

复制代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>石家庄铁道大学在校学生行程信息统计</title>
    
    <script>        
function static_num() 
{            
    document.getElementById("btnOperate").onclick = function () {                
        var arr = new Array();                
        var items = document.getElementsByName("category");                 
        for (i = 0; i < items.length; i++) {                    
            if (items[i].checked) {                        
                arr.push(items[i].value);                    
            }                
        }                 
        alert("选择的个数为:" + arr.length);            
    };        
};    
    </script>

</head>
<body>
    <form action="later.jsp" method="get">
        <table align="center" border="1" width="500">
            <tr>
                <td>1.姓名 </td>
                <td>
                    <input type="text" name="name" />
                </td>
            </tr>
            
            <tr>
                <td>2.学号</td>
                <td>
                    <input type="text" name="ID" />
                </td>
            </tr>
            
            <tr>
                <td>3.学生类别</td>
                <td>
                    <form action="">
                        <input type="radio" name="studentType" ID="">本科生
                        <input type="radio" name="studentType" ID="">研究生
                    </form>
                </td>
            </tr>
            
            <tr>
                <td>4.院系</td>
                <td>
                    <form>
                        <select>
                            <option value="土木学院">土木学院</option>
                            <option value="机械学院">机械学院</option>
                            <option value="交通学院">交通学院</option>
                            <option value="信息学院">信息学院</option>
                            <option value="经管学院">经管学院</option>
                        </select>
                    </form>
                </td>
            </tr>
            
            <tr>
                <td>5.联系电话 </td>
                <td>
                    <input type="text" name="phonenum" />
                </td>
            </tr>
            
            <tr>
                <td>6.健康码颜色 </td>
                <td>
                    <form action="">
                        <input type="radio" name="color" ID="">绿码
                        <input type="radio" name="color" ID="">黄码
                        <input type="radio" name="color" ID="">红码
                    </form>
                </td>
            </tr>
            
            <tr>
                <td>7.行程统计 </td>
                <td>
                    <p><input type="checkbox" name="category" value="□10月30日去过人民医院" />□10月30日去过人民医院 </p>   
                    <p><input type="checkbox" name="category" value="□10月25日以来去过深泽县人民医院" />□10月25日以来去过深泽县人民医院</p>
                    <p><input type="checkbox" name="category" value="□10月16日以来去过深泽县庄泽村" />□10月16日以来去过深泽县庄泽村</p>    
                    <p><input type="checkbox" name="category" value="□10月29日以来去过黑龙江哈尔滨市或者黑河市" />□10月29日以来去过黑龙江哈尔滨市或者黑河市</p>    
                    <p><input type="checkbox" name="category" value="□10月18日以来途径贵州遵义市;北京丰台、昌平" />□10月18日以来途径贵州遵义市;北京丰台、昌平</p>    
                    <p><input type="checkbox" name="category" value="□10月17日以来到过湖南长沙;青海海东市" />□10月17日以来到过湖南长沙;青海海东市</p>        
                    <p><input type="checkbox" name="category" value="□无" />□无 </p> 
                    <p><input id="btnOperate" type="" value="" onclick="static_num()" /></p>
                </td>
            </tr>
            
            <tr>
                <td>8.其他涉疫信息需要填报的 </td>
                <td>
                    <input type="text" name="other" />
                </td>
            </tr>
            <tr align="center">
                <td colspan="2">
                    <input type="submit" value="提交" />
                </td>
            </tr>
        </table>
    </form>
</body>
</html>
复制代码

later.jsp文件:

复制代码
<%@page import="dao.Dao"%>
<%@page import="model.Model"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<%
    //接收客户端传递过来的参数
    String name = request.getParameter("name");
    int ID = Integer.parseInt(request.getParameter("ID"));
    String studentType = request.getParameter("studentType");
    String collage = request.getParameter("collage");
    String phonenum = request.getParameter("phonenum");
    String color = request.getParameter("color");
    String xingcheng = request.getParameter("xingcheng");
    String other = request.getParameter("other");
    

    try
    {
        Model m = new Model();
        m.setName(name);
        m.setID(ID);
        m.setStudentType(studentType);
        m.setCollage(collage);
        m.setPhonenum(phonenum);
        m.setColor(color);
        m.setXingcheng(xingcheng);
        m.setOther(other);
        Dao d = new Dao();
        d.add(m);
    }
    catch(Exception e)
    {
%>
    <h2 style="color:red ; font-size:50px">发生错误 : <%=e.getMessage() %></h2>
<%
    }
%>

</html>
复制代码

 

posted @   kuaiquxie  阅读(234)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」
点击右上角即可分享
微信分享提示