基於 MVC 模式實現簡單 航班查詢系統

一、前言

本文 MVC 模式的一個簡單案例,可以作爲 練習 MVC 模式進行測試,建議不瞭解 MVC 模式 的小夥伴可以先去學習一下。

二、功能要求:

要求實現根據航班號查詢航班信息的功能
(a) 初始面面爲查詢頁面,用戶在該頁面輸入要查詢的航班號,如圖1所示
在這裏插入圖片描述
(b) 用戶輸入航班號,點擊“搜索航班”按鈕時,系統將校驗用戶輸入內容,當用戶沒有輸入航班號直接點擊“搜索航班”按鈕時,將給出提示信息,如圖2所示
在這裏插入圖片描述

© 用戶輸入航班號並點擊“搜索航班”按鈕後,系統提交該查詢請求,並在查詢結果頁面上顯示滿足條件的航班信息,如圖3所示
在這裏插入圖片描述

(d) 當系統沒有找到該航班的信息時,在查詢頁面上顯示提示信息。用戶點擊“返回”按鈕時,頁面回到查詢頁面,如圖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&amp&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 的案例就實現了,需要的小夥伴可以參考一下,參考的同時記得修改相關的包名等內容哦!如果該文對您有幫助,別忘了點個贊,點個關注哦!
感謝感謝!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章