一、任务介绍
现在有一家航空公司为了提高用户体验,希望做一个航班信息系统,用户
可以根据需求去对航班信息进行操作。组长把这个任务安排给了程序员赵丹,
赵丹发现这里需要通过 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
4月 20, 2021 1:53:00 下午 com.alibaba.druid.pool.DruidDataSource error
严重: testWhileIdle is true, validationQuery not set
4月 20, 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