数据库_航班信息系统任务

一、任务介绍

现在有一家航空公司为了提高用户体验,希望做一个航班信息系统,用户
可以根据需求去对航班信息进行操作。组长把这个任务安排给了程序员赵丹,
赵丹发现这里需要通过 java 代码操作数据库,并且用户是可以在控制台做对
应的操作,JDBC 可以帮她解决这个问题。学习起来,试着把这个系统实现出
来。

二、实现

1.数据准备-数据库

首先创建一个数据库,查询如下:

mysql> select * from airinfo;
+----+--------+-------------+------------+
| id | number | destination | startdate  |
+----+--------+-------------+------------+
|  1 | 0001   | 北京        | 2021-02-15 |
|  2 | 0002   | 上海        | 2021-08-04 |
|  3 | 0003   | 日本        | 2021-01-05 |
|  4 | 0004   | 广州        | 2021-05-05 |
|  5 | 0005   | 台湾        | 2021-06-04 |
+----+--------+-------------+------------+
5 rows in set (0.00 sec)

2.基础类包bean

遵循类名=表名,属性名=列名的规范,定义类:

package bean;

import javax.xml.crypto.Data;
import java.sql.Date;

public class AirInfo {

    private int id;
    private String number;
    private String destination;
    private Date startDate;

    public int getId() {
        return id;
    }

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

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public String getDestination() {
        return destination;
    }

    public void setDestination(String destination) {
        this.destination = destination;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }
}

3.属性文件properties

存储数据库的用户名、密码、驱动类地址、数据库链接

driverclass=com.mysql.cj.jdbc.Driver
uname=root
upass=027050
url=jdbc:mysql://localhost:3306/plane?serverTimezone=UTC

4.工具包util

工具类,定义链接数据库方法、查询方法、增删改方法、关闭数据库方法

// 工具类
public class BaseDao {

    // 1.定义变量
    private Connection connection;
    private PreparedStatement pps;
    private ResultSet resultSet;// 存储查询结果
    private int count;// 存储增删改结果

    private static String userName;
    private static String userPass;
    private static String url;
    private static String driverName;
    private static DruidDataSource dataSource = new DruidDataSource();

    // 2.加载驱动(静态代码块之执行一次)
    static {
        ResourceBundle bundle = ResourceBundle.getBundle("db");
        driverName = bundle.getString("driverclass");
        url = bundle.getString("url");
        userName = bundle.getString("uname");
        userPass = bundle.getString("upass");

        // 德鲁伊连接池
        dataSource.setUsername(userName);
        dataSource.setPassword(userPass);
        dataSource.setUrl(url);
        dataSource.setDriverClassName(driverName);
    }

    // 3.方法一:获得连接
    protected Connection getConnection(){
        try {
            connection = dataSource.getConnection();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return connection;
    }

    // 4.方法二:获得预状态通道
    protected PreparedStatement getPps(String sql){
        try {
            pps = getConnection().prepareStatement(sql);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return pps;
    }

    // 5.方法三:绑定参数(预装态通道内的?)(list保存需要替代?的值的容器)
    protected void param(List list){
        if(list != null && list.size()>0){
            for (int i = 0;i<list.size();i++) {
                try {
                    pps.setObject(i + 1, list.get(i));
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }

    }

    // 6.方法四:增删改结果
    protected int update(String sql,List list){
        getPps(sql);
        param(list);
        try {
            count = pps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return count;
    }

    // 7.方法五:查询结果
    protected ResultSet query(String sql,List list){
        getPps(sql);
        param(list);
        try {
            resultSet = pps.executeQuery();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return resultSet;
    }

    // 8.方法六:关闭资源
    protected void closeAll(){
        try {
            if (resultSet != null) {
                resultSet.close();
            }
            if (pps != null) {
                pps.close();
            }
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}

5.功能实现dao

查看所有航班信息、按起飞时间查询、按目的地查询、删除航班、更新航班,四个方法的接口及实现类
接口:

public interface AirInfoDao {

    // 1.查询所有航班信息
    public List<AirInfo> viewAll();

    // 2.按起飞时间查询
    public AirInfo getByDate(Date startDate);

    // 3.按目的地查询
    public AirInfo getByDestination(String destination);

    // 4.删除航班
    public Boolean delete (int id);

    // 5.更新航班
    public Boolean update(int id);
}

实现类:

package dao.daoimpl;

import bean.AirInfo;
import dao.AirInfoDao;
import util.BaseDao;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class AirInfoDaoImpl extends BaseDao implements AirInfoDao {
    @Override
    public List<AirInfo> viewAll() {
        List<AirInfo> airInfoList = new ArrayList<>();

        try {
            String sql = "select * from airinfo";
            List list =new ArrayList();
            ResultSet rs = query(sql,list);

            while(rs.next()){
                AirInfo airInfo = new AirInfo();
                airInfo.setId(rs.getInt("id"));
                airInfo.setNumber(rs.getString("number"));
                airInfo.setDestination(rs.getString("destination"));
                airInfo.setStartDate(rs.getDate("startdate"));
                airInfoList.add(airInfo);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }

        return airInfoList;
    }

    // 2.通过日期查询航班
    @Override
    public List<AirInfo> getByDate(Date startDate) {
        List<AirInfo> airInfoList = new ArrayList<>();

        try {
            String sql = "select * from airinfo where startdate=?";
            List list = new ArrayList();
            list.add(startDate);
            ResultSet rs = query(sql, list);
            while(rs.next()){
                AirInfo airInfo = new AirInfo();
                airInfo.setId(rs.getInt("id"));
                airInfo.setNumber(rs.getString("number"));
                airInfo.setDestination(rs.getString("destination"));
                airInfo.setStartDate(rs.getDate("startdate"));
                airInfoList.add(airInfo);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }

        // 返回数据
            return airInfoList;
    }

    // 通过目的地查询航班
    @Override
    public List<AirInfo> getByDestination(String destination) {
        List<AirInfo> airInfoList = new ArrayList<>();

        try {
            String sql = "select * from airinfo where destination=?";
            List list = new ArrayList();
            list.add(destination);
            ResultSet rs = query(sql, list);
            while(rs.next()){
                AirInfo airInfo = new AirInfo();
                airInfo.setId(rs.getInt("id"));
                airInfo.setNumber(rs.getString("number"));
                airInfo.setDestination(rs.getString("destination"));
                airInfo.setStartDate(rs.getDate("startdate"));
                airInfoList.add(airInfo);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }

        // 返回数据
        return airInfoList;
    }

    // 删除航班
    @Override
    public Boolean delete(int id) {
        String sql = "delete from airinfo where id=?";
        List list = new ArrayList();
        list.add(id);
        int count = update(sql,list);
        if(count > 0){
            return true;// 删除成功
        }
        return false;// 删除失败
    }

    // 更新航班
    @Override
    public Boolean update(int id,List messageList) {
                // 更新
                String sql = "update airinfo set number=?,destination=?,startdate=? where id=?";
                messageList.add(id);

                // 结果
                int count = update(sql, messageList);
                if (count > 0) {
                    return true;
                }

        return false;
    }

    // 更新航班重载,判断航班是否存在
    public Boolean update(int id) {

        // 初始化
        String sql0= "select * from airinfo where id=?";
        AirInfo airInfo = new AirInfo();
        List list0 = new ArrayList();
        list0.add(id);
        ResultSet resultSet = query(sql0,list0);

        try {
            if(resultSet.next()) {// 存在数据时执行更新
                    return true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return false;
    }
}

6.功能测试包test

测试各个方法是否有效

(1)查询所有航班信息

public class Test1 {
    public static void main(String[] args) {

        AirInfoDao airInfoDao =new AirInfoDaoImpl();
        List<AirInfo> airInfoList = new ArrayList<AirInfo>();
        airInfoList = airInfoDao.viewAll();
        for (AirInfo airInfo : airInfoList) {
            System.out.println("编号:"+ airInfo.getId()+"\t航班号"+ airInfo.getNumber()+"\t目的地"+ airInfo.getDestination()+"\t起飞时间"+ airInfo.getStartDate());
        }

    }

}

打印:

编号:1	航班号0001	目的地北京	起飞时间2021-02-15
编号:2	航班号0002	目的地上海	起飞时间2021-08-04
编号:3	航班号0003	目的地日本	起飞时间2021-01-05
编号:4	航班号0004	目的地广州	起飞时间2021-05-05
编号:5	航班号0005	目的地台湾	起飞时间2021-06-04

Process finished with exit code 0

(2)通过日期查询航班信息

public static void main(String[] args) throws ParseException {

        AirInfoDao airInfoDao =new AirInfoDaoImpl();

        String str = "2021-08-04";
        Date date = null;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        date = format.parse(str);
        java.sql.Date dayDateSql = new java.sql.Date(date.getTime());

        List<AirInfo> airInfoList = airInfoDao.getByDate(dayDateSql);
        for (AirInfo airInfo : airInfoList) {
            System.out.println("编号:"+ airInfo.getId()+"\t航班号"+ airInfo.getNumber()+"\t目的地"+ airInfo.getDestination()+"\t起飞时间"+ airInfo.getStartDate());
        }

    }
}

运行结果:

编号:2	航班号0002	目的地上海	起飞时间2021-08-04

Process finished with exit code 0

(3)通过目的地查询航班信息

public class Test3 {

    public static void main(String[] args) throws ParseException {

        AirInfoDao airInfoDao =new AirInfoDaoImpl();

        String str = "北京";

        List<AirInfo> airInfoList = airInfoDao.getByDestination(str);
        for (AirInfo airInfo : airInfoList) {
            System.out.println("编号:"+ airInfo.getId()+"\t航班号"+ airInfo.getNumber()+"\t目的地"+ airInfo.getDestination()+"\t起飞时间"+ airInfo.getStartDate());
        }

    }
}

打印:

编号:1	航班号0001	目的地北京	起飞时间2021-02-15

Process finished with exit code 0

(4)通过id删除航班

删除前:

mysql> select * from airinfo;
+----+--------+-------------+------------+
| id | number | destination | startdate  |
+----+--------+-------------+------------+
|  1 | 0001   | 北京        | 2021-02-15 |
|  2 | 0002   | 上海        | 2021-08-04 |
|  3 | 0003   | 日本        | 2021-01-05 |
|  4 | 0004   | 广州        | 2021-05-05 |
|  5 | 0005   | 台湾        | 2021-06-04 |
|  6 | 0006   | 海南        | 2021-09-24 |
+----+--------+-------------+------------+
6 rows in set (0.00 sec)
// 删除航班
public class Test4 {

    public static void main(String[] args) throws ParseException {

        AirInfoDao airInfoDao =new AirInfoDaoImpl();

        if(airInfoDao.delete(6)){
            System.out.println("删除成功");
        }else{
            System.out.println("删除失败");
        }
    }
}

打印:

删除成功

删除完成:

mysql> select * from airinfo;
+----+--------+-------------+------------+
| id | number | destination | startdate  |
+----+--------+-------------+------------+
|  1 | 0001   | 北京        | 2021-02-15 |
|  2 | 0002   | 上海        | 2021-08-04 |
|  3 | 0003   | 日本        | 2021-01-05 |
|  4 | 0004   | 广州        | 2021-05-05 |
|  5 | 0005   | 台湾        | 2021-06-04 |
+----+--------+-------------+------------+
5 rows in set (0.00 sec)

(5)更新航班信息

更新前:

mysql> select * from airinfo;
+----+--------+-------------+------------+
| id | number | destination | startdate  |
+----+--------+-------------+------------+
|  1 | 0001   | 北京        | 2021-02-15 |
|  2 | 0002   | 上海        | 2021-08-04 |
|  3 | 0003   | 日本        | 2021-01-05 |
|  4 | 0004   | 广州        | 2021-05-05 |
|  5 | 0005   | 台湾        | 2021-06-04 |
+----+--------+-------------+------------+
5 rows in set (0.00 sec)

代码:

public class Test5 {

    public static void main(String[] args) throws ParseException {
        AirInfoDaoImpl af =new AirInfoDaoImpl();

        // 更新前检查id是否存在对应的数据
        if(af.update(3)) {
            List list = new ArrayList();
            list.add("0003");
            list.add("日本");
            list.add("2021-01-05");

            if (af.update(3, list)) {
                System.out.println("更新成功");
            } else {
                System.out.println("更新失败");
            }
        }
    }
}

打印:

更新成功

更新后:

mysql> select * from airinfo;
+----+--------+-------------+------------+
| id | number | destination | startdate  |
+----+--------+-------------+------------+
|  1 | 0001   | 北京        | 2021-02-15 |
|  2 | 0002   | 上海        | 2021-08-04 |
|  3 | 0003   | 本日        | 2021-01-05 |
|  4 | 0004   | 广州        | 2021-05-05 |
|  5 | 0005   | 台湾        | 2021-06-04 |
+----+--------+-------------+------------+
5 rows in set (0.00 sec)

7.人机交互界面Ui

// 人机交互界面
public class Ui {

    // 属性一:获取用户输入
    private Scanner input = new Scanner(System.in);

    // 方法一:欢迎界面
    public void welcome(){
        System.out.println("==========欢迎使用航班信息管理系统==========");
    }

    // 方法二:功能选择
    public int functionSelect(){
        System.out.println("请选择操作:1-查询所有航班,2-按起飞时间查询,3-按目的地查询,4-删除航班,5-更新航班,6-离开系统");
        String str = input.nextLine();
        int select = -1;
        try {
            select = Integer.parseInt(str);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return select;
    }

    // 方法三:输入错误提示
    public void errorSelect(){
        System.out.println("输入错误,请重新选择");
    }

    // 方法四:查询所有航班信息(按日期查询、按目的地查询也用此方法)
    public void viewAll(List<AirInfo> list){
        System.out.println("编号\t\t航班号\t\t目的地\t\t起飞日期");
        for (AirInfo airInfo : list) {
            System.out.println(airInfo.getId()+"\t\t"+ airInfo.getNumber()+"\t\t"+ airInfo.getDestination()+"\t\t\t"+ airInfo.getStartDate());
        }
    }

    // 方法五:获取日期
    public Date getDate(){

        System.out.println("请输入要查询的日期:");
        String str = input.nextLine();
        java.util.Date date = null;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            date = format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        java.sql.Date dayDateSql = new java.sql.Date(date.getTime());

        return dayDateSql;
    }

    // 方法六:获取目的地
    public String getDestination(){

        System.out.println("请输入要查询的目的地:");
        String str = input.nextLine();
        return str;
    }

    // 方法七:删除获取Id
    public int getIdDelete(){
        System.out.println("请输入要删除的航班的id:");
        String str = input.nextLine();
        int id = -1;
        try {
            id = Integer.parseInt(str);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return id;
    }

    // 方法八:修改获取Id
    public int getIdUpdate(){
        System.out.println("请输入要更新的航班的id");
        String str = input.nextLine();
        int id = -1;
        try {
            id = Integer.parseInt(str);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return id;
    }

    // 方法九:更新航班,获取list
    public List messageList(){
        List list = new ArrayList();
        System.out.println("请输入新的航班号:");
        list.add(input.nextLine());
        System.out.println("请输入新的目的地:");
        list.add(input.nextLine());
        System.out.println("请输入新的起飞日期:");
        list.add(input.nextLine());
        return list;
    }

    // 方法十:成功提示
    public void success(){
        System.out.println("操作成功");
    }

    // 方法十一:失败提示
    public void fail(){
        System.out.println("操作失败,请检查航班信息及网络状况");
    }

    // 方法十一:再见
    public void bye(){
        System.out.println("本次服务到此结束,祝您生活愉快!");
    }

    // 方法十二:对应id不存在时提示
    public void without(){
        System.out.println("无此id对应的航班");
    }

}

8.主程序main

public class AirInfoMain {

    // 属性一:操作方法类
    private  static AirInfoDaoImpl airInfoDao = new AirInfoDaoImpl();
    // 属性二:人机交互界面
    private static Ui ui = new Ui();
    public static void main(String[] args) {

        // 欢迎界面
        ui.welcome();
        // 循环执行
        h:while(true){
            switch (ui.functionSelect()){
                case 1:// 查询所有航班
                    viewAll();
                    break ;
                case 2:// 按起飞时间查询
                    queryByDate();
                    break ;
                case 3:// 按目的地查询
                    queryByDestination();
                    break ;
                case 4:// 删除航班
                    delete();
                    break ;
                case 5:// 更新航班
                    update();
                    break ;
                case 6:// 离开系统
                    break h;
                default:
                    ui.errorSelect();
            }
        }
        // 结束程序
        ui.bye();
    }

    // 方法一:查询航班
    private static void viewAll(){
        List<AirInfo> list = airInfoDao.viewAll();
        ui.viewAll(list);
    }

    // 方法二:按起飞日期查询
    private static void queryByDate(){
        // 获取日期
        Date date = ui.getDate();
        // 查询
        List<AirInfo> list = airInfoDao.getByDate(date);
        ui.viewAll(list);
    }

    // 方法三:按起目的地
    private static void queryByDestination(){
        // 获取目的地
        String destination = ui.getDestination();
        // 查询
        List<AirInfo> list = airInfoDao.getByDestination(destination);
        ui.viewAll(list);
    }

    // 方法四:删除航班
    private static void delete(){
        // 获取id
        int id = ui.getIdDelete();
        // 删除
        if(airInfoDao.delete(id)){
            ui.success();
        }else {
            ui.fail();
        }
    }

    // 方法五:更新航班
    private static void update(){
        // 获取id
        int id = ui.getIdUpdate();
        // 判断id是否存在
        if(airInfoDao.update(id)) {
            // 获取更新表
            List list = ui.messageList();
            if (airInfoDao.update(id, list)){
                // 成功提示
                ui.success();
            }else {
                ui.fail();
            }
        }else {
            ui.without();
        }
    }

}

运行效果:

==========欢迎使用航班信息管理系统==========
请选择操作:1-查询所有航班,2-按起飞时间查询,3-按目的地查询,4-删除航班,5-更新航班,6-离开系统
1
420, 2021 1:53:00 下午 com.alibaba.druid.pool.DruidDataSource error
严重: testWhileIdle is true, validationQuery not set
420, 2021 1:53:00 下午 com.alibaba.druid.pool.DruidDataSource info
信息: {dataSource-1} inited
编号		航班号		目的地		起飞日期
1		0001		北京			2021-02-15
2		0002		上海			2021-08-04
3		0003		日本			2021-01-05
4		0004		广州			2021-05-05
5		0005		台湾			2021-06-04
6		0006		湖州			2021-08-20
请选择操作:1-查询所有航班,2-按起飞时间查询,3-按目的地查询,4-删除航班,5-更新航班,6-离开系统
2
请输入要查询的日期:
2021-02-15
编号		航班号		目的地		起飞日期
1		0001		北京			2021-02-15
请选择操作:1-查询所有航班,2-按起飞时间查询,3-按目的地查询,4-删除航班,5-更新航班,6-离开系统
3
请输入要查询的目的地:
北京
编号		航班号		目的地		起飞日期
1		0001		北京			2021-02-15
请选择操作:1-查询所有航班,2-按起飞时间查询,3-按目的地查询,4-删除航班,5-更新航班,6-离开系统
4
请输入要删除的航班的id:
6
操作成功
请选择操作:1-查询所有航班,2-按起飞时间查询,3-按目的地查询,4-删除航班,5-更新航班,6-离开系统
5
请输入要更新的航班的id
5
请输入新的航班号:
0005
请输入新的目的地:
台湾省
请输入新的起飞日期:
2021-09-09
操作成功
请选择操作:1-查询所有航班,2-按起飞时间查询,3-按目的地查询,4-删除航班,5-更新航班,6-离开系统
1
编号		航班号		目的地		起飞日期
1		0001		北京			2021-02-15
2		0002		上海			2021-08-04
3		0003		日本			2021-01-05
4		0004		广州			2021-05-05
5		0005		台湾省			2021-09-09
请选择操作:1-查询所有航班,2-按起飞时间查询,3-按目的地查询,4-删除航班,5-更新航班,6-离开系统
6
本次服务到此结束,祝您生活愉快!

Process finished with exit code 0
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
本系统查询结果完全和中航信数据同步。完全实时。保证准确无误。说明:本查询系统包括 查询 预订 管理 三部份。可以查询国内。国际航班.包括单程,返程系统特点:1.不是传统的小偷查询。是真正访问中航信数据库。保证数据实时准确2.机票预订功能。查询完成。可以直接预订。管理员后台查看管理3.后台管理强大。可定制航空公司.航空城市.运价管理.常规航位.机型管理.星期表.会员管理.燃油附加等等。极大提高了系统灵活性!4.包括国内机票查询。国际机票查询。以及单程.返程查询本版新加功能:1.加入在线支付功能.支持10几家银行。支付更容易。支付完成实时冲值。2.加入国内。国际航班高级查询。国内高级查询:fly_search.asp.国际高级查询:fly_search_ab.asp3.方便为用户冲值。可在后台进行。入款。扣款操作安装步骤============================================运行环境:IIS (虚拟主机,本地。独立服务器都可以) 1.把flight目录放到网站根目录下 确保http://localhost/flight 能正常访问2.注意flight不能改名。否则将不能正常查询3.确保访问地址是http://localhost/flight (查询结果只返回到http://localhost/flight/show.asp)=============================================4.后台登陆页面:system_manage/default.htm 管理初始用户:admin 密码:8888885.后台机票接口基本参数设置的服务器地址,客户ID,MD5值。请不要随意更改。否则将不能正确查询6.数据库目录clientdb/RCclientdb.mdb.为了系统的安全,可自行修改相设置。如果修改数据库路径及名字。请在conn.asp做相应该修改top.asp 头文件请自行添加更改bottom.asp 尾文件请自行添加更改会员系统:在目录 order/register/ 链接即可 index.asp为注册会员,index.asp为登陆页面。测试会员号:abc 密码:123

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值