基于 MVC 模式实现简单 航班查询系统

文章目录

    • 一、前言
    • 二、功能要求:
    • 三、代码展示:
      • View:
      • Model层:
      • Cotroller层:
    • 四、效果展示:
    • 后记:

一、前言

本文 MVC 模式的一个简单案例,可以作为 练习 MVC 模式进行测试,建议不了解 MVC 模式 的小伙伴可以先去学习一下。

二、功能要求:

要求实现根据航班号查询航班信息的功能
(a) 初始面面为查询页面,用户在该页面输入要查询的航班号,如图1所示
基于 MVC 模式实现简单 航班查询系统_第1张图片
(b) 用户输入航班号,点击“搜索航班”按钮时,系统将校验用户输入内容,当用户没有输入航班号直接点击“搜索航班”按钮时,将给出提示信息,如图2所示
基于 MVC 模式实现简单 航班查询系统_第2张图片

© 用户输入航班号并点击“搜索航班”按钮后,系统提交该查询请求,并在查询结果页面上显示满足条件的航班信息,如图3所示
基于 MVC 模式实现简单 航班查询系统_第3张图片

(d) 当系统没有找到该航班的信息时,在查询页面上显示提示信息。用户点击“返回”按钮时,页面回到查询页面,如图4所示
基于 MVC 模式实现简单 航班查询系统_第4张图片

三、代码展示:

View:

index.jsp:


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>

        <form action="FlightServlet" method="post">

            <span style="color: red">${space}</span> <br>
            <h2>航班信息查询</h2>
            请输入航班号:<input type="text" name = "flightNumber">

            <input type="submit" value="搜索航班">

        </form>

  </body>
</html>

info.jsp


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
查询结果:

<c:choose>
    <c:when test="${requestScope.error == null }">
        <table border="2">
            <tr bgcolor="red">
                <td>航班号</td>
                <td>航空公司</td>
                <td>出发机场</td>
                <td>到达机场</td>
                <td>出发时间</td>
                <td>到达时间</td>
                <td>机型</td>

            </tr>

            <tr>
                <td>${flightinfo.flightid}</td>
                <td>${flightinfo.company}</td>
                <td>${flightinfo.leaveairport}</td>
                <td>${flightinfo.arriveairport}</td>
                <td>${flightinfo.leavetime}</td>
                <td>${flightinfo.arrivetime}</td>
                <td>${flightinfo.airplane}</td>
            </tr>

        </table>
    </c:when>
    <c:otherwise>
        ${error}
    </c:otherwise>
    
</c:choose>
<a href="index.jsp">回到首页</a>

</body>
</html>

Model层:

==实体部分JavaBean:对应 数据库中表。 ==

package rj.entity;

public class FlightInfo {
    private int id;
    private String flightid;
    private String company;
    private String leaveairport;
    private String arriveairport;
    private String leavetime;
    private String arrivetime;
    private String airplane;

    public FlightInfo(){};

    public FlightInfo(String flightid, String company, String leaveairport, String arriveairport, String leavetime, String arrivetime, String airplane) {
        this.flightid = flightid;
        this.company = company;
        this.leaveairport = leaveairport;
        this.arriveairport = arriveairport;
        this.leavetime = leavetime;
        this.arrivetime = arrivetime;
        this.airplane = airplane;
    }

    public FlightInfo(int id, String flightid, String company, String leaveairport, String arriveairport, String leavetime, String arrivetime, String airplane) {
        this.id = id;
        this.flightid = flightid;
        this.company = company;
        this.leaveairport = leaveairport;
        this.arriveairport = arriveairport;
        this.leavetime = leavetime;
        this.arrivetime = arrivetime;
        this.airplane = airplane;
    }

    public int getId() {
        return id;
    }

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

    public String getFlightid() {
        return flightid;
    }

    public void setFlightid(String flightid) {
        this.flightid = flightid;
    }

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

    public String getLeaveairport() {
        return leaveairport;
    }

    public void setLeaveairport(String leaveairport) {
        this.leaveairport = leaveairport;
    }

    public String getArriveairport() {
        return arriveairport;
    }

    public void setArriveairport(String arriveairport) {
        this.arriveairport = arriveairport;
    }

    public String getLeavetime() {
        return leavetime;
    }

    public void setLeavetime(String leavetime) {
        this.leavetime = leavetime;
    }

    public String getArrivetime() {
        return arrivetime;
    }

    public void setArrivetime(String arrivetime) {
        this.arrivetime = arrivetime;
    }

    public String getAirplane() {
        return airplane;
    }

    public void setAirplane(String airplane) {
        this.airplane = airplane;
    }

    @Override
    public String toString() {
        return "FlightInfo{" +
                "flightid='" + flightid + '\'' +
                ", company='" + company + '\'' +
                ", leaveairport='" + leaveairport + '\'' +
                ", arriveairport='" + arriveairport + '\'' +
                ", leavetime='" + leavetime + '\'' +
                ", arrivetime='" + arrivetime + '\'' +
                ", airplane='" + airplane + '\'' +
                '}';
    }
}

Dao 层:

package rj.dao;

import rj.entity.FlightInfo;
import rj.util.DBUtil;

import java.sql.ResultSet;
import java.sql.SQLException;

public class FlightDao {


    public FlightInfo findByFlightNumber(String flightNumber) throws SQLException {

        String sql = "select * from flightinfo where flightid = ?";

        Object[] params = {flightNumber};


        ResultSet rs = DBUtil.excucateQuery(sql, params);

        while(rs.next()) {

           String flightid = rs.getString("flightid");
           String company = rs.getString("company");
           String leaveairport = rs.getString("leaveairport");
           String arriveairport = rs.getString("arriveairport");
           String leavetime = rs.getString("leavetime");
           String arrivetime = rs.getString("arrivetime");
           String airplane = rs.getString("airplane");

            FlightInfo flightInfo = new FlightInfo(flightid,company,leaveairport,arriveairport,leavetime,arrivetime,airplane);
            System.out.println(flightInfo);
            return flightInfo;
        }

        return null;
    }

}

DBUtil:数据库工具类:

package rj.util;

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

/*
 * 数据库帮助类:可以简化代码(将每个模块中重复的代码弄到一个方法中,增加代码复用)
 * DBUtil: 简化 Dao 层的代码
 *
 * */

public class DBUtil {

    private static final String URL = "jdbc:mysql://localhost:3306/flight?useSSL=false&&serverTimezone=UTC";
    private static final String User = "root";
    private static final String Password = "root";

    public static Connection conn = null;
    public static PreparedStatement pstam = null;
    public static ResultSet rs = null;

    public static Connection getConnection() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.cj.jdbc.Driver") ;
        return  DriverManager.getConnection( URL,User,Password ) ;
    }
    public static PreparedStatement createPreParedStatement(String sql,Object[] params) throws ClassNotFoundException, SQLException {
        pstam = getConnection().prepareStatement(sql) ;
        if(params!=null ) {
            for(int i=0;i<params.length;i++) {
                pstam.setObject(i+1, params[i]);
            }
        }
        return pstam;
    }

    public static boolean exeucateUpdate(String sql,Object[] pstams) {
        int result = 0;
        try {
            pstam = createPreParedStatement(sql,pstams);
            result = pstam.executeUpdate();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                if(rs != null) rs.close();
                if(pstam != null) pstam.close();
                if(conn != null) conn.close();
            } catch(SQLException e) {
                e.printStackTrace();
            }
        }

        if(result > 0) return true;
        else return false;
    }
    public static ResultSet excucateQuery(String sql, Object[] pstams) {

        try {
            pstam = createPreParedStatement(sql,pstams);
            rs = pstam.executeQuery();
            return rs;

        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}

Cotroller层:

业务逻辑层:

package rj.servlet;

import rj.dao.FlightDao;
import rj.entity.FlightInfo;

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 java.io.IOException;
import java.sql.SQLException;

@WebServlet(urlPatterns = "/FlightServlet")
public class FlightServlet extends HttpServlet {

    private FlightDao flightDao = new FlightDao();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String flightNumber = request.getParameter("flightNumber");
        System.out.println("...............................................................................");
        System.out.println(flightNumber);

        if(flightNumber.equals("") || flightNumber.trim().equals("")) {
            request.setAttribute("space","Sorry,您还没有输入航班号哦!");
            request.getRequestDispatcher("/index.jsp").forward(request,response);
            return;
        }

        try {

            FlightInfo flightinfo = flightDao.findByFlightNumber(flightNumber);

            if(flightinfo != null) {
                request.setAttribute("flightinfo",flightinfo);
            } else {
                request.setAttribute("error","没有该航班信息!");
            }
            request.getRequestDispatcher("/info.jsp").forward(request,response);

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

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}

四、效果展示:

基于 MVC 模式实现简单 航班查询系统_第5张图片
基于 MVC 模式实现简单 航班查询系统_第6张图片
基于 MVC 模式实现简单 航班查询系统_第7张图片
注意:除了MVC案例。在此赠送一套最新Java架构项目实战教程+大厂面试题库,想学的 点击此处免费获取,小白勿进哦

后记:

到此,一个 MVC 的案例就实现了,需要的小伙伴可以参考一下,参考的同时记得修改相关的包名等内容哦!如果该文对您有帮助,别忘了点个赞,点个关注哦!
感谢感谢!

你可能感兴趣的:(项目管理)