Java实现航空航班管理系统
本文实例为大家分享了Java实现航空航班管理系统的具体代码,供大家参考,具体内容如下
Plane对象:
在Plane对象里定义了飞机的编号id、航班号、目的地、起飞日期。生成了构造方法和toString()方法;以及getting()和setting()方法,但在程序里没用到。
package com.hangkong; public class Plane { private int id;//编号 private String planeNum;//航班号 private String address;//目的地 private String date;//日期 public Plane(int id, String planeNum, String address, String date) { super(); this.id = id; this.planeNum = planeNum; this.address = address; this.date = date; } public Plane(){ super(); } //Alt+Shift+s public int getId() { return id; } public void setId(int id) { this.id = id; } public String getPlaneNum() { return planeNum; } public void setPlaneNum(String planeNum) { this.planeNum = planeNum; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } @Override public String toString() { return "Plane" + id + "\t\t" + planeNum + "\t\t" + address + "\t\t" + date; //return "Plane ID:" + id + "\t航班编号:" + planeNum + "\t目的地:" + address + "\t起飞时间:" + date; } }
MySQL数据库:
数据库名字是Fly,数据表是plane;在getcon()函数中注册驱动、获取连接
SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for `plane` -- ---------------------------- DROP TABLE IF EXISTS `plane`; CREATE TABLE `plane` ( `id` int(20) NOT NULL AUTO_INCREMENT, `planeNum` varchar(20) DEFAULT NULL, `address` varchar(20) DEFAULT NULL, `date` varchar(20) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of plane -- ---------------------------- INSERT INTO `plane` VALUES ('1', 'DZ001', '东京', '2019-9-1'); INSERT INTO `plane` VALUES ('2', 'DZ002', '上海', '2019-8-28'); INSERT INTO `plane` VALUES ('3', 'DZ003', '广州', '2019-8-29'); INSERT INTO `plane` VALUES ('4', 'DZ004', '深圳', '2019-8-29'); INSERT INTO `plane` VALUES ('5', 'DZ005', '厦门', '2019-8-30'); INSERT INTO `plane` VALUES ('6', 'DZ006', '杭州', '2019-8-30'); INSERT INTO `plane` VALUES ('7', 'DZ007', '武汉', '2019-8-30'); INSERT INTO `plane` VALUES ('8', 'DZ008', '成都', '2019-8-30'); INSERT INTO `plane` VALUES ('9', 'DZ009', '西安', '2019-8-30'); INSERT INTO `plane` VALUES ('10', 'DZ0010', '郑州', '2019-8-30'); INSERT INTO `plane` VALUES ('11', 'DZ0011', '长沙', '2019-8-30'); INSERT INTO `plane` VALUES ('12', 'DZ0012', '民权', '2019-8-31'); INSERT INTO `plane` VALUES ('13', 'DZ0013', '莫斯科', '2019-9-1'); INSERT INTO `plane` VALUES ('14', 'DZ0014', '曼谷', '2019-9-2'); INSERT INTO `plane` VALUES ('15', 'DZ0015', '阿布扎比', '2019-9-2');
主程序TestFly:
TestFly类中有实现各种功能的函数,包括 1.列出所有航班,2.按起飞时间查询,3.按目的地查询,4.删除航班,5.更新航班,6.增加航班,7.退出系统。
package com.hangkong; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.HashSet; import java.util.Scanner; import java.util.Set; import org.omg.CosNaming.NamingContextExtPackage.StringNameHelper; import com.mysql.jdbc.ExceptionInterceptor; import com.sun.javafx.runtime.VersionInfo; import com.sun.xml.internal.ws.api.pipe.NextAction; public class TestFly { static Connection con = null;//连接 static PreparedStatement ps = null;//模板 static ResultSet rs = null;//结果集 public static void main(String[] args) throws Exception { System.out.println("******************************************大壮航空航班信息管理系统********************************************\n"); //show(); boolean bool = Dome(); while(bool){ bool = Dome(); } if(!bool){ System.out.println("**************************************已成功退出大壮航空航班信息管理系统**************************************\n"); System.exit(0); } } //流程 public static boolean Dome() throws Exception{ Scanner scan = new Scanner(System.in); show(); int key = scan.nextInt(); switch (key) { case 1: showMessage(listMessage()); break; case 2:{ System.out.println("输入起飞时间:"); String date = scan.next(); showMessage(selectDate(date)); }break; case 3:{ System.out.println("输入目的地:"); String Address = scan.next(); showMessage(selectAddress(Address)); }break; case 4:{ System.out.println("输入航班编号:"); String planeNum = scan.next(); deleteFly(planeNum); }break; case 5:{ System.out.println("输入航班编号和更改后目的地和时间:"); String planeNum = scan.next(); String Address = scan.next(); String date = scan.next(); updateFly(Address,date,planeNum); }break; case 6:{ System.out.println("输入航班编号、目的地、起飞时间:"); String planeNum = scan.next(); String Address = scan.next(); String date = scan.next(); creatPlane(planeNum,Address,date); }break; default: //scan.close(); return false; } //scan.close(); return true; } //注册驱动,获取连接 public static Connection getCon() throws Exception{ Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://localhost:3306/fly", "root", ""); return con; } //创建初始信息,插入信息 public static void creatPlane(String planeNum,String address, String date) throws Exception{ getCon(); String sql = "insert into plane values (null,?,?,?)"; ps = con.prepareStatement(sql); ps.setString(1, planeNum); ps.setString(2, address); ps.setString(3, date); ps.executeUpdate(); ps.close(); con.close(); selectPlaneNum(planeNum); } //系统主菜单 public static void show(){ System.out.println("请选择操作:(1.列出所有航班,2.按起飞时间查询,3.按目的地查询,4.删除航班,5.更新航班,6.增加航班,7.退出系统)"); } //获取结果集合输出 public static void showMessage(Set<Plane> set){ System.out.println("\n********************************大壮航空***********************************\n"); if(set.size() == 0){ System.out.println("未匹配到任何数据!"); System.out.println("\n********************************大壮航空***********************************\n"); return; } System.out.println("Plane\t\t航班编号\t目的地\t\t起飞时间"); for( Plane value : set){ System.out.println(value); } System.out.println("\n********************************大壮航空***********************************\n"); } //列出所有航班信息 public static Set<Plane> listMessage() throws Exception{ getCon(); String sql = "select * from plane"; ps = con.prepareStatement(sql); rs = ps.executeQuery(); Set<Plane> set = new HashSet<>(); while(rs.next()){ int id = rs.getInt("id"); String planeNum = rs.getString("planeNum"); String address = rs.getString("address"); String dateTime = rs.getString("date"); Plane plane = new Plane(id, planeNum, address, dateTime); set.add(plane); } ps.close(); con.close(); return set; } //按起飞时间查询 public static Set<Plane> selectDate(String date) throws Exception{ getCon(); String sql = "select * from plane where date = ? "; ps = con.prepareStatement(sql); ps.setString(1, date); rs = ps.executeQuery(); Set<Plane> set = new HashSet<>(); //String planes = ""; while(rs.next()){ int id = rs.getInt("id"); String planeNum = rs.getString("planeNum"); String address = rs.getString("address"); String dateTime = rs.getString("date"); Plane plane = new Plane(id, planeNum, address, dateTime); set.add(plane); //planes += plane.toString() + "\n"; } ps.close(); con.close(); return set; } //按目的地查询 public static Set<Plane> selectAddress(String Address) throws Exception{ getCon(); String sql = "select * from plane where address = ? "; ps = con.prepareStatement(sql); ps.setString(1, Address); rs = ps.executeQuery(); Set<Plane> set = new HashSet<>(); //String planes = ""; while(rs.next()){ int id = rs.getInt("id"); String planeNum = rs.getString("planeNum"); String address = rs.getString("address"); String dateTime = rs.getString("date"); Plane plane = new Plane(id, planeNum, address, dateTime); set.add(plane); //planes += plane.toString() + "\n"; } ps.close(); con.close(); return set; } //按航班编号 public static void selectPlaneNum(String planeNum) throws Exception{ getCon(); String sql = "select * from plane where planeNum = ? "; ps = con.prepareStatement(sql); ps.setString(1, planeNum); rs = ps.executeQuery(); boolean x = true; while(rs.next()){ if(x){ System.out.println("\n********************************大壮航空***********************************\n"); System.out.println("Plane\t\t航班编号\t目的地\t\t起飞时间"); } int id = rs.getInt("id"); String planenum = rs.getString("planeNum"); String address = rs.getString("address"); String date = rs.getString("date"); System.out.println("Plane" + id + "\t\t" + planenum + "\t\t" + address + "\t\t" + date); x = false; } System.out.println("\n********************************大壮航空***********************************\n"); } //按航班编号删除航班 public static void deleteFly(String planeNum) throws Exception{ getCon(); String sql = "delete from plane where planeNum = ? "; ps = con.prepareStatement(sql); ps.setString(1, planeNum); ps.executeUpdate(); ps.close(); con.close(); System.out.println("\n********************************大壮航空***********************************\n"); System.out.println("已删除!"); System.out.println("\n********************************大壮航空***********************************\n"); } //按航班编号更新航班目的地和时间 public static void updateFly(String Address,String date,String planeNum) throws Exception{ getCon(); String sql = "update plane set address = ?,date = ? where planeNum = ? "; ps = con.prepareStatement(sql); ps.setString(1, Address); ps.setString(2, date); ps.setString(3, planeNum); ps.executeUpdate(); ps.close(); con.close(); selectPlaneNum(planeNum); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)