做个小项目那不是简简单单!Java 实现航空航班管理系统。
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");
}
评论