飞机航班系统
一、语言和环境
1.实现语言:JSP
2.环境要求:Eclipse+SqlServer|Oracle|MySql
二、数据库设计
数据库名称:flight
数据库表的结构:
表名flightinfo实体名称航班信息表
主键id
序号字段名称字段说明类型位数属性.备注
1idID列int非空标识列,自增
2flightid航班号varchar10非空唯一约束
3company航空公司varchar20非空
4leaveairport出发机场varchar20非空
5arriveairport到达机场varchar20非空
6leavetime出发时间varchar10非空
7arrivetime到达时间varchar10非空
8airplane机型varchar20非空
数据表中数据:
flightidcompanyleaveairportarriveairportleavetimearrivetimeairplane
CA1433中国国航首都机场荷花机场18:2020:40波音737
CZ3718南方航空首都机场荷花机场13:2515:55空客321
KN2920联合航空荷花机场北京机场18:0020:10波音737
三、功能要求
航班查询系统要求实现根据航班号查询航班信息的功能。
1)查询页面,用户在该页面的文本输入查询的航班号,如图1所示:
2)用户输入航班号,点击“搜索航班”按钮时,系统将校验用户输入内容,当用户没有
输入航班号直接点击“搜索航班”按钮时,将给出提示信息,如图2所示:
3)用户输入航班号并点击“搜索航班”按钮后,系统提交该查询请求,并在查询结果页
面上显示满足条件的航班信息,如图3所示:
4)当系统没有找到该航班的信息时,在查询页面上显示提示信息。用户点击“返回”
按钮时,页面回到查询页面,如图4所示
四、注意事项
1.仔细审题,把题目要求理解准确;
2.请注意按照界面的设计要求来进行窗体设计;
3.请注意代码的书写、命名规范和适当的注释。
<%--
Created by IntelliJ IDEA.
User: 未进化的类人猿
Date: 2020/5/29
Time: 10:41
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>航班信息查询</title>
<script>
window.onclick = function () {
document.getElementById("submit").onclick = function () {
var flag = false;
var value = document.getElementById("sid").value;
if(value == ""){
flag = true;
}
if(!flag){
document.getElementById("form").submit();
}else{
alert("请输入航班号!");
}
}
}
</script>
</head>
<body>
<h1>航班信息查询</h1>
<form action="${pageContext.request.contextPath}/searchServlet" method="post" id="form">
<p><label for="sid">请输入航班号:</label> <input type="text" name="input" id="sid" placeholder="请输入您的航班号"/></p>
<p><input type="submit" value="搜索航班" id="submit"/></p>
</form>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: 未进化的类人猿
Date: 2020/5/29
Time: 11:49
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>查询结果</title>
<style>
th{
color: red;
}
a{
display: block;
width: 55px;
height: 25px;
text-align: center;
line-height: 25px;
text-decoration: none;
margin-top: 15px;
background: black;
color: white;
border: 1px solid #cccccc;
}
</style>
</head>
<body>
<h1>查询结果</h1>
<c:forEach items="${list}" var="list" varStatus="s">
<h3>${list.flightid}次航班信息:</h3>
</c:forEach>
<table border="2" cellpadding="5" cellspacing="0">
<tr>
<th>航空公司</th>
<th>出发机场</th>
<th>到达机场</th>
<th>出发</th>
<th>到达</th>
<th>机型</th>
</tr>
<c:forEach items="${list}" var="list" varStatus="s">
<tr>
<td>${list.company}</td>
<td>${list.leaveairport}</td>
<td>${list.arriveairport}</td>
<td>${list.leavetime}</td>
<td>${list.arrivetime}</td>
<td>${list.airplane}</td>
</tr>
</c:forEach>
</table>
<a href="index.jsp">返回</a>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: 未进化的类人猿
Date: 2020/5/29
Time: 12:04
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>查询结果</title>
<style>
a{
display: block;
width: 55px;
height: 25px;
text-align: center;
line-height: 25px;
text-decoration: none;
margin-top: 15px;
background: black;
color: white;
border: 1px solid #cccccc;
}
</style>
</head>
<body>
<h1>查询结果</h1>
<h3>没有找到该航班的信息!</h3>
<a href="index.jsp">返回</a>
</body>
</html>
package main.java.dao;
import main.java.domain.Flight;
import java.util.List;
public interface SearchDao {
public List<Flight> findAll(String flightid);
}
package main.java.dao.impl;
import main.java.dao.SearchDao;
import main.java.domain.Flight;
import main.java.utils.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
public class SearchDaoImpl implements SearchDao {
private JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtils.getDataSource());
@Override
public List<Flight> findAll(String flightid) {
String sql = "select * from flightinfo where flightid = ?";
return jdbcTemplate.query(sql,new BeanPropertyRowMapper<Flight>(Flight.class),flightid);
}
}
package main.java.domain;
/**
* id INT NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT 'ID系列',
* flightid VARCHAR(10) NOT NULL UNIQUE COMMENT '航班号',
* company VARCHAR(20) NOT NULL COMMENT '航空公司',
* leaveairport VARCHAR(20) NOT NULL COMMENT '出发机场',
* arriveairport VARCHAR(20) NOT NULL COMMENT '到达机场',
* leavetime VARCHAR(10) NOT NULL COMMENT '出发时间',
* arrivetime VARCHAR(10) NOT NULL COMMENT '到达时间',
* airplane VARCHAR(20) NOT NULL COMMENT '机型'
*/
public class Flight {
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 Flight() {
}
public Flight(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 "Flight{" +
"id=" + id +
", flightid='" + flightid + '\'' +
", company='" + company + '\'' +
", leaveairport='" + leaveairport + '\'' +
", arriveairport='" + arriveairport + '\'' +
", leavetime='" + leavetime + '\'' +
", arrivetime='" + arrivetime + '\'' +
", airplane='" + airplane + '\'' +
'}';
}
}
package main.java.servlet;
import main.java.domain.Flight;
import java.util.List;
public interface SearchServlet {
public List<Flight> findAll(String flightid);
}
package main.java.servlet.impl;
import main.java.dao.SearchDao;
import main.java.dao.impl.SearchDaoImpl;
import main.java.domain.Flight;
import main.java.servlet.SearchServlet;
import java.util.List;
public class SearchServletImpl implements SearchServlet {
private SearchDao searchDao = new SearchDaoImpl();
@Override
public List<Flight> findAll(String flightid) {
return searchDao.findAll(flightid);
}
}
package main.java.utils;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
/**
* JDBC工具类 使用Durid连接池
*/
public class JDBCUtils {
private static DataSource ds ;
static {
try {
//1.加载配置文件
Properties pro = new Properties();
//使用ClassLoader加载配置文件,获取字节输入流
InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");
pro.load(is);
//2.初始化连接池对象
ds = DruidDataSourceFactory.createDataSource(pro);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取连接池对象
*/
public static DataSource getDataSource(){
return ds;
}
/**
* 获取连接Connection对象
*/
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
}
package main.java.web.service;
import main.java.domain.Flight;
import main.java.servlet.impl.SearchServletImpl;
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.util.List;
@WebServlet("/searchServlet")
public class SearchServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;utf-8");
String input = request.getParameter("input");
main.java.servlet.SearchServlet searchServlet = new SearchServletImpl();
List<Flight> list = searchServlet.findAll(input);
System.out.println(list);
if(list.toString().equals("[]")){
response.sendRedirect(request.getContextPath()+"/fail.jsp");
}else{
request.setAttribute("list",list);
request.getRequestDispatcher("/list.jsp").forward(request,response);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///flight
username=root
password=root
# 初始化连接数量
initialSize=5
# 最大连接数
maxActive=10
# 最大等待时间
maxWait=3000
CREATE DATABASE flight;
USE flight;
CREATE TABLE flightinfo(
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT 'ID系列',
flightid VARCHAR(10) NOT NULL UNIQUE COMMENT '航班号',
company VARCHAR(20) NOT NULL COMMENT '航空公司',
leaveairport VARCHAR(20) NOT NULL COMMENT '出发机场',
arriveairport VARCHAR(20) NOT NULL COMMENT '到达机场',
leavetime VARCHAR(10) NOT NULL COMMENT '出发时间',
arrivetime VARCHAR(10) NOT NULL COMMENT '到达时间',
airplane VARCHAR(20) NOT NULL COMMENT '机型'
)ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
DESC flightinfo;
SELECT * FROM flightinfo;
SELECT * FROM flightinfo WHERE flightid = "CA1433";