做个小项目那不是简简单单!Java 实现航空航班管理系统。
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;
}
}
数据库名字是 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 类中有实现各种功能的函数,包括 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()){
评论