Java实现Web航空订票系统(servlet+jdbc+jsp+mysql)

create table airline(

code varchar(3) not null primary key,

name varchar(20) not null,

discount float(2,2) not null

);

create table airport(

code varchar(4) not null primary key,

name varchar(20) not null,

city varchar(20) not null,

country varchar(20) not null,

connTime int(3) not null /* minute */

);

create table airplane(

name varchar(20) not null primary key,

type varchar(10) not null

);

create table seat(

id int(3) not null primary key auto_increment,

relativeID int(3) not null, /* 飞机上作为的相对编号 */

row int(3) not null,

num int(3) not null,

type varchar(12) not null, /* windowSeat middleSeat aisleSeat*/

flightID int(10) not null, /* Table:airplane Item:id */

passport varchar(20) /* passenger passport */

);

create table flight(

id int(10) not null primary key,

airlineCode varchar(3) not null,

number int(6) not null,

depatureDate Date not null,

depatureTime time not null,

arrivalDate Date not null,

arrivalTime time not null,

fare float(6,2) not null,

depatureAirport varchar(4) not null, /* Table:airport Item:code */

arrivalAirport varchar(4) not null, /* Table:airport Item:code */

airplaneName varchar(20) not null, /* Table:airplane Item:name */

airplaneType varchar(10) not null /* Table:airplane Item:type */

);

use AIRLINE_2;

create table airline(

code varchar(3) not null primary key,

name varchar(20) not null,

discount float(2,2) not null

);

create table airport(

code varchar(4) not null primary key,

name varchar(20) not null,

city varchar(20) not null,

country varchar(20) not null,

connTime int(3) not null /* minute */

);

create table airplane(

name varchar(20) not null primary key,

type varchar(10) not null

);

create table seat(

id int(3) not null primary key auto_increment,

relativeID int(3) not null, /* 飞机上作为的相对编号 */

row int(3) not null,

num int(3) not null,

type varchar(12) not null, /* windowSeat middleSeat aisleSeat*/

flightID int(10) not null, /* Table:airplane Item:id */

passport varchar(20) /* passenger passport */

);

create table flight(

id int(10) not null primary key,

airlineCode varchar(3) not null,

number int(6) not null,

depatureDate Date not null,

depatureTime time not null,

arrivalDate Date not null,

arrivalTime time not null,

fare float(6,2) not null,

depatureAirport varchar(4) not null, /* Table:airport Item:code */

arrivalAirport varchar(4) not null, /* Table:airport Item:code */

airplaneName varchar(20) not null, /* Table:airplane Item:name */

airplaneType varchar(10) not null /* Table:airplane Item:type */

);

use AIRLINE_3;

create table airline(

code varchar(3) not null primary key,

name varchar(20) not null,

discount float(2,2) not null

);

create table airport(

code varchar(4) not null primary key,

name varchar(20) not null,

city varchar(20) not null,

country varchar(20) not null,

connTime int(3) not null /* minute */

);

create table airplane(

name varchar(20) not null primary key,

type varchar(10) not null

);

create table seat(

id int(3) not null primary key auto_increment,

relativeID int(3) not null, /* 飞机上作为的相对编号 */

row int(3) not null,

num int(3) not null,

type varchar(12) not null, /* windowSeat middleSeat aisleSeat*/

flightID int(10) not null, /* Table:airplane Item:id */

passport varchar(20) /* passenger passport */

);

create table flight(

id int(10) not null primary key,

airlineCode varchar(3) not null,

number int(6) not null,

depatureDate Date not null,

depatureTime time not null,

arrivalDate Date not null,

arrivalTime time not null,

fare float(6,2) not null,

depatureAirport varchar(4) not null, /* Table:airport Item:code */

arrivalAirport varchar(4) not null, /* Table:airport Item:code */

airplaneName varchar(20) not null, /* Table:airplane Item:name */

airplaneType varchar(10) not null /* Table:airplane Item:type */

);

/* create table of database ABS*/

use ABS;

create table airline(

code varchar(3) not null primary key,

name varchar(20) not null,

discount float(2,2) not null

);

create table flightInfo(

id int(10) not null primary key auto_increment,

airlineCode varchar(3) not null, /* Table:airline Item:code */

flightID int(10) not null, /* Database:Airline Table:flight Item: */

number int(6) not null, /* Database:Airline Table:flight Item:number */

depatureDate Date not null, /* Database:Airline Table:flight Item:depatureDate */

depatureTime time not null, /* Database:Airline Table:flight Item:depatureTime */

arrivalDate Date not null, /* Database:Airline Table:flight Item:arrivalDate */

arrivalTime time not null, /* Database:Airline Table:flight Item:arrivalTime */

fare float(6,2) not null, /* Database:Airline Table:flight Item:fare */

depatureAirport varchar(4) not null, /* Database:Airline Table:flight Item:depatureAirport */

arrivalAirport varchar(4) not null, /* Database:Airline Table:flight Item:arrivalAirport */

airlineName varchar(20) not null, /* Table:airline Item:name */

airplaneName varchar(20) not null, /* Database:Airline Table:airplane Item:name */

airplaneType varchar(10) not null, /* 大:240 中:160 小:80 */

depatureAirportName varchar(20) not null,

arrivalAirportName varchar(20) not null,

depatureAirportCity varchar(20) not null,

arrivalAirportCity varchar(20) not null,

airplaneEmptySeats int(3) not null /* Database:Airline Table:airplane Item:conunt of empty seats */

);

create table airport(

code varchar(4) not null primary key,

name varchar(20) not null,

city varchar(20) not null,

country varchar(20) not null,

connTime int(3) not null /* minute */

);

create table passenger(

passport varchar(20) not null, /* no primary key for test */

name varchar(20) not null

);

create table trip(

id int(10) not null primary key,

flightInfoID int(10) not null, /* Database:Airline Table:flight Item: */

fare float(6,2) not null, /* fare不同于 flightInfo中的 fare */

passport varchar(20) not null, /* Table:passenger Item:passport */

seatID int(3) not null /* Database:Airline Table:seat Item:relativeID */

);

create table orders(

id int(10) not null, /* id 相同的为同一个订单 */

tripID int(10) not null, /* Table:tirp Item:id */

createDate date not null,

createTime time not null,

totalFare float(6,2) not null,

contactName varchar(20) not null,

contactPhone varchar(20) not null

);

二、系统展示

=====================================================================

1.机票查询


在这里插入图片描述

2.选择航班


在这里插入图片描述

3.填写乘客和联系人信息、提交订单


在这里插入图片描述

4.订单详细信息


在这里插入图片描述

三、部分代码

=====================================================================

SearchAction


package com.abs.action;

import java.sql.Date;

import java.sql.Time;

import java.util.ArrayList;

import java.util.List;

import java.util.regex.*;

import com.abs.db.DBName;

import com.abs.factory.DaoFactory;

import com.abs.model.*;

public class SearchAction {

public static List searchFlightDirectly(String departureCity, String arrivalCity, String departureDateString) throws Exception {

List list = null;

Date departureDate = null;

if (checkDate(departureDateString)) { // 测试Date格式是否合格

departureDate = Date.valueOf(departureDateString);

}

list = DaoFactory.getFlightInfoDaoInstance(DBName.ABS).findByAirport(departureCity, arrivalCity, departureDate);

return list;

}

public static List<List> searchFlightTransfer(String departureCity, String arrivalCity, String departureDateString) throws Exception {

List<List> list = new ArrayList<List>();

Date departureDate = null;

if (checkDate(departureDateString)) { // 测试Date格式是否合格

departureDate = Date.valueOf(departureDateString);

}

List departureList = DaoFactory.getFlightInfoDaoInstance(DBName.ABS).findByDepatureAirport(departureCity, departureDate);

for (FlightInfo depatureFlight : departureList) {

Airport transferAirport = DaoFactory.getAirportDaoInstance(DBName.ABS).findByCode(depatureFlight.getArrivalAirport());

if(null != transferAirport){

int connTime = transferAirport.getConnTime();

Time depatureTime = new Time(depatureFlight.getArrivalTime().getTime() + connTime * 60 * 1000);

String transferCity = depatureFlight.getArrivalAirportCity();

List arrivalList = DaoFactory.getFlightInfoDaoInstance(DBName.ABS).findByTransferArrivalAirport(transferCity, arrivalCity, departureDate, depatureTime);

for (FlightInfo arrivalFlight : arrivalList) {

List item = new ArrayList();

if(depatureFlight.getAirlineCode().equals(arrivalFlight.getAirlineCode())){

double discount = DaoFactory.getAirlineDaoInstance(DBName.ABS).findByCode(depatureFlight.getAirlineCode()).getDiscount();

arrivalFlight.setFare(arrivalFlight.getFare() * discount);

}

item.add(depatureFlight);

item.add(arrivalFlight);

list.add(item);

}

}else {

System.err.println(depatureFlight.getArrivalAirportCity() + depatureFlight.getArrivalAirport() + “:没有找到机场信息”);

}

}

return list;

}

public static boolean checkDate(String date) {

String regex= “^((\d{2

  • 12
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MySQL是一个常用的关系型数据库管理系统,可以通过Java中的JDBC技术来连接和操作MySQL数据库。在Java Web应用中,可以通过ServletJSP技术来实现MySQL数据库的增删改查操作。 以下是一个简单的示例,演示如何使用MySQL JDBC连接MySQL数据库,并通过ServletJSP实现数据的增删改查操作。 1.导入MySQL JDBC驱动程序 在Java Web项目中,需要将MySQL JDBC驱动程序添加到项目的classpath中,以便在程序运行时可以动态加载它。可以通过Maven或手动下载MySQL JDBC驱动程序来实现。 2.连接MySQL数据库 在Servlet中,可以使用Java中的JDBC技术来连接MySQL数据库。以下是一个示例代码: ``` import java.sql.*; public class DBUtil { private static final String DRIVER = "com.mysql.jdbc.Driver"; private static final String URL = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8"; private static final String USERNAME = "root"; private static final String PASSWORD = "root"; public static Connection getConnection() throws ClassNotFoundException, SQLException { Class.forName(DRIVER); return DriverManager.getConnection(URL, USERNAME, PASSWORD); } } ``` 在上面的代码中,使用了Java中的JDBC技术来连接MySQL数据库。其中,DRIVER是MySQL JDBC驱动程序的类路径,URL是MySQL数据库的连接字符串,USERNAME和PASSWORD是连接MySQL数据库所需要的用户名和密码。 3.实现增删改查操作 在Servlet中,可以使用Java中的JDBC技术来实现MySQL数据库的增删改查操作。以下是一个示例代码: ``` import java.io.IOException; import java.sql.*; import javax.servlet.ServletException; import javax.servlet.http.*; public class UserServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String action = request.getParameter("action"); if ("add".equals(action)) { addUser(request, response); } else if ("delete".equals(action)) { deleteUser(request, response); } else if ("update".equals(action)) { updateUser(request, response); } else if ("query".equals(action)) { queryUser(request, response); } } private void addUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); Connection conn = null; PreparedStatement stmt = null; try { conn = DBUtil.getConnection(); stmt = conn.prepareStatement("insert into user(username, password) values(?, ?)"); stmt.setString(1, username); stmt.setString(2, password); stmt.executeUpdate(); response.sendRedirect(request.getContextPath() + "/index.jsp"); } catch (Exception e) { e.printStackTrace(); } finally { DBUtil.close(stmt, conn); } } private void deleteUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String id = request.getParameter("id"); Connection conn = null; PreparedStatement stmt = null; try { conn = DBUtil.getConnection(); stmt = conn.prepareStatement("delete from user where id=?"); stmt.setInt(1, Integer.parseInt(id)); stmt.executeUpdate(); response.sendRedirect(request.getContextPath() + "/index.jsp"); } catch (Exception e) { e.printStackTrace(); } finally { DBUtil.close(stmt, conn); } } private void updateUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String id = request.getParameter("id"); String username = request.getParameter("username"); String password = request.getParameter("password"); Connection conn = null; PreparedStatement stmt = null; try { conn = DBUtil.getConnection(); stmt = conn.prepareStatement("update user set username=?, password=? where id=?"); stmt.setString(1, username); stmt.setString(2, password); stmt.setInt(3, Integer.parseInt(id)); stmt.executeUpdate(); response.sendRedirect(request.getContextPath() + "/index.jsp"); } catch (Exception e) { e.printStackTrace(); } finally { DBUtil.close(stmt, conn); } } private void queryUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; try { conn = DBUtil.getConnection(); stmt = conn.prepareStatement("select * from user"); rs = stmt.executeQuery(); request.setAttribute("users", rs); request.getRequestDispatcher("/user.jsp").forward(request, response); } catch (Exception e) { e.printStackTrace(); } finally { DBUtil.close(rs, stmt, conn); } } } ``` 在上面的代码中,实现了一个UserServlet,其中包含了addUser、deleteUser、updateUser和queryUser四个方法,分别对应MySQL数据库的增、删、改、查操作。在每个方法中,首先获取请求参数,然后使用Java中的JDBC技术连接MySQL数据库,最后执行相应的SQL语句。如果操作成功,则跳转到相应的页面,否则输出错误信息。 4.使用JSP页面显示数据 在JSP页面中,可以通过EL表达式和JSTL标签来显示MySQL数据库中的数据。以下是一个示例代码: ``` <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <html> <head> <title>用户列表</title> </head> <body> <table border="1"> <tr> <th>ID</th> <th>用户名</th> <th>密码</th> </tr> <c:forEach var="user" items="${users}"> <tr> <td>${user.id}</td> <td>${user.username}</td> <td>${user.password}</td> </tr> </c:forEach> </table> </body> </html> ``` 在上面的代码中,使用了JSTL标签来遍历MySQL数据库中的数据,并使用EL表达式来显示数据。如果数据为空,则不会显示任何内容。 以上就是使用MySQL JDBCServletJSP技术实现MySQL数据库的增删改查操作的示例。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值