commit 35ec24f3441902878d47db1104696999b91a9645 Author: Qing Date: Mon Jun 5 23:52:15 2023 +0800 Init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8eb6870 --- /dev/null +++ b/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ +.idea +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/JavaWeb-Flight-System.iml b/JavaWeb-Flight-System.iml new file mode 100644 index 0000000..b4c3af3 --- /dev/null +++ b/JavaWeb-Flight-System.iml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/libs/commons-lang3-3.12.0.jar b/libs/commons-lang3-3.12.0.jar new file mode 100644 index 0000000..4d434a2 Binary files /dev/null and b/libs/commons-lang3-3.12.0.jar differ diff --git a/libs/jakarta.servlet.jsp.jstl-2.0.0.jar b/libs/jakarta.servlet.jsp.jstl-2.0.0.jar new file mode 100644 index 0000000..92712b0 Binary files /dev/null and b/libs/jakarta.servlet.jsp.jstl-2.0.0.jar differ diff --git a/libs/jakarta.servlet.jsp.jstl-api-2.0.0.jar b/libs/jakarta.servlet.jsp.jstl-api-2.0.0.jar new file mode 100644 index 0000000..81059ec Binary files /dev/null and b/libs/jakarta.servlet.jsp.jstl-api-2.0.0.jar differ diff --git a/libs/mysql-connector-j-8.0.31.jar b/libs/mysql-connector-j-8.0.31.jar new file mode 100644 index 0000000..8b74bb8 Binary files /dev/null and b/libs/mysql-connector-j-8.0.31.jar differ diff --git a/libs/servlet-api.jar b/libs/servlet-api.jar new file mode 100644 index 0000000..1321455 Binary files /dev/null and b/libs/servlet-api.jar differ diff --git a/src/com/landaiqing/dao/FlightDao.java b/src/com/landaiqing/dao/FlightDao.java new file mode 100644 index 0000000..941b479 --- /dev/null +++ b/src/com/landaiqing/dao/FlightDao.java @@ -0,0 +1,174 @@ +package com.landaiqing.dao; + +import com.landaiqing.entity.FlightEntity; +import com.landaiqing.utils.JDBCUtil; + +import java.sql.*; +import java.util.ArrayList; +import java.util.List; + +public class FlightDao { + /* + * 查询所有的航班信息 + * */ + public List getByAll() { + Connection connection = null; + PreparedStatement preparedStatement = null; + ResultSet resultSet = null; + try { + connection = JDBCUtil.getConnection(); + preparedStatement = connection.prepareStatement("select * from flight where is_delete=0;"); + resultSet = preparedStatement.executeQuery(); + ArrayList flightEntities = new ArrayList<>(); + while (resultSet.next()) { + Integer id = resultSet.getInt("id"); + String flightId = resultSet.getString("flight_id"); + String company = resultSet.getString("company"); + String departureAirport = resultSet.getString("departure_airport"); + String arriveAirport = resultSet.getString("arrive_airport"); + Date departureTime = resultSet.getDate("departure_time"); + Date arriveTime = resultSet.getDate("arrive_time"); + String model = resultSet.getString("model"); + Integer isDelete = resultSet.getInt("is_delete"); + FlightEntity flightEntity = new FlightEntity(id, flightId, company, departureAirport, arriveAirport, departureTime, arriveTime, model, isDelete); + flightEntities.add(flightEntity); + } + return flightEntities; + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + JDBCUtil.closeConnection(resultSet, preparedStatement, connection); + } + + } + + public int deleteFlghtById(Integer id) { + Connection connection = null; + PreparedStatement preparedStatement = null; + try { + connection = JDBCUtil.getConnection(); + JDBCUtil.beginTransaction(connection); + preparedStatement = connection.prepareStatement("delete from flight where id=?;"); + preparedStatement.setInt(1, id); + Integer result = preparedStatement.executeUpdate(); + JDBCUtil.commitTransaction(connection); + return result; + + } catch (SQLException e) { + JDBCUtil.rollBackTransaction(connection); + throw new RuntimeException(e); + + } finally { + JDBCUtil.closeConnection(null, preparedStatement, connection); + } + } + + public FlightEntity getByIdFlight(Integer id) { + + Connection connection = null; + PreparedStatement preparedStatement = null; + ResultSet resultSet = null; + try { + connection = JDBCUtil.getConnection(); + preparedStatement = connection.prepareStatement("select * from flight where id=?;"); + preparedStatement.setInt(1, id); + resultSet = preparedStatement.executeQuery(); + if (!resultSet.next()) { + return null; + } + Integer dbId = resultSet.getInt("id"); + String flightId = resultSet.getString("flight_id"); + String company = resultSet.getString("company"); + String departureAirport = resultSet.getString("departure_airport"); + String arriveAirport = resultSet.getString("arrive_airport"); + Date departureTime = resultSet.getDate("departure_time"); + Date arriveTime = resultSet.getDate("arrive_time"); + String model = resultSet.getString("model"); + Integer isDelete = resultSet.getInt("is_delete"); + FlightEntity flightEntity = new FlightEntity(dbId, flightId, company, departureAirport, arriveAirport, departureTime, arriveTime, model, isDelete); + + return flightEntity; + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + JDBCUtil.closeConnection(resultSet, preparedStatement, connection); + } + } + public int updateFlight(FlightEntity flightEntity){ + Connection connection = null; + PreparedStatement preparedStatement = null; + try { + connection = JDBCUtil.getConnection(); + JDBCUtil.beginTransaction(connection); + preparedStatement = connection.prepareStatement("UPDATE `testdb`.`flight` SET `flight_id` = ?, `company` = ?, `departure_airport` = ?, `arrive_airport` = ?, `departure_time` = ?, `arrive_time` = ?, `model` = ?, `is_delete` = '0' WHERE `id` = ?;"); + preparedStatement.setString(1,flightEntity.getFlightId()); + preparedStatement.setString(2,flightEntity.getCompany()); + preparedStatement.setString(3,flightEntity.getDepartureAirport()); + preparedStatement.setString(4,flightEntity.getArriveAirport()); + preparedStatement.setDate(5, new Date(flightEntity.getDepartureTime().getTime())); + preparedStatement.setDate(6, new Date(flightEntity.getArriveTime().getTime())); + preparedStatement.setString(7,flightEntity.getModel()); + preparedStatement.setInt(8,flightEntity.getId()); + Integer result = preparedStatement.executeUpdate(); + JDBCUtil.commitTransaction(connection); + return result; + + } catch (SQLException e) { + JDBCUtil.rollBackTransaction(connection); + throw new RuntimeException(e); + + } finally { + JDBCUtil.closeConnection(null, preparedStatement, connection); + } + } + + public int insertFlight(FlightEntity flightEntity){ + Connection connection = null; + PreparedStatement preparedStatement = null; + try { + connection = JDBCUtil.getConnection(); + JDBCUtil.beginTransaction(connection); + preparedStatement = connection.prepareStatement("INSERT INTO `testdb`.`flight` (`id`, `flight_id`, `company`, `departure_airport`, `arrive_airport`, `departure_time`, `arrive_time`, `model`, `is_delete`) VALUES (null, ?, ?, ?, ?, ?, ?, ?, '0');"); + preparedStatement.setString(1,flightEntity.getFlightId()); + preparedStatement.setString(2,flightEntity.getCompany()); + preparedStatement.setString(3,flightEntity.getDepartureAirport()); + preparedStatement.setString(4,flightEntity.getArriveAirport()); + preparedStatement.setDate(5, new Date(flightEntity.getDepartureTime().getTime())); + preparedStatement.setDate(6, new Date(flightEntity.getArriveTime().getTime())); + preparedStatement.setString(7,flightEntity.getModel()); +// preparedStatement.setInt(8,flightEntity.getId()); + Integer result = preparedStatement.executeUpdate(); + JDBCUtil.commitTransaction(connection); + return result; + + } catch (SQLException e) { + JDBCUtil.rollBackTransaction(connection); + throw new RuntimeException(e); + + } finally { + JDBCUtil.closeConnection(null, preparedStatement, connection); + } + } + + public int updateDelete(Integer id){ + Connection connection = null; + PreparedStatement preparedStatement = null; + try { + connection = JDBCUtil.getConnection(); + JDBCUtil.beginTransaction(connection); + preparedStatement = connection.prepareStatement("UPDATE `testdb`.`flight` SET `is_delete` = ? WHERE `id` = ?;"); + preparedStatement.setInt(1,1); + preparedStatement.setInt(2,id); + Integer result = preparedStatement.executeUpdate(); + JDBCUtil.commitTransaction(connection); + return result; + + } catch (SQLException e) { + JDBCUtil.rollBackTransaction(connection); + throw new RuntimeException(e); + + } finally { + JDBCUtil.closeConnection(null, preparedStatement, connection); + } + } +} diff --git a/src/com/landaiqing/entity/FlightEntity.java b/src/com/landaiqing/entity/FlightEntity.java new file mode 100644 index 0000000..e22ddb9 --- /dev/null +++ b/src/com/landaiqing/entity/FlightEntity.java @@ -0,0 +1,117 @@ +package com.landaiqing.entity; + +import java.util.Date; + +public class FlightEntity { + /* + * CREATE TABLE `flight` ( + `id` int NOT NULL AUTO_INCREMENT COMMENT 'id列', + `flight_id` varchar(20) DEFAULT NULL COMMENT '航班号', + `company` varchar(255) DEFAULT NULL COMMENT '航空公司', + `departure_airport` varchar(255) DEFAULT NULL COMMENT '出发机场', + `arrive_airport` varchar(255) DEFAULT NULL COMMENT '到达机场', + `departure_time` datetime DEFAULT NULL COMMENT '出发时间', + `arrive_time` datetime DEFAULT NULL COMMENT '到达时间', + `model` varchar(255) DEFAULT NULL COMMENT '机型', + `is_delete` varchar(255) DEFAULT NULL COMMENT '是否隐藏0显示1隐藏', + PRIMARY KEY (`id`) +) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; + * */ + private Integer id; + private String flightId; + + private String company; + private String departureAirport; + private String arriveAirport; + private Date departureTime; + private Date arriveTime; + private String model; + private Integer isDelete; + + public FlightEntity(){ + + } + public FlightEntity(Integer id, String flightId, String company, String departureAirport, String arriveAirport, Date departureTime, Date arriveTime, String model, Integer isDelete) { + this.id = id; + this.flightId = flightId; + this.company = company; + this.departureAirport = departureAirport; + this.arriveAirport = arriveAirport; + this.departureTime = departureTime; + this.arriveTime = arriveTime; + this.model = model; + this.isDelete = isDelete; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getFlightId() { + return flightId; + } + + public void setFlightId(String flightId) { + this.flightId = flightId; + } + + public String getCompany() { + return company; + } + + public void setCompany(String company) { + this.company = company; + } + + public String getDepartureAirport() { + return departureAirport; + } + + public void setDepartureAirport(String departureAirport) { + this.departureAirport = departureAirport; + } + + public String getArriveAirport() { + return arriveAirport; + } + + public void setArriveAirport(String arriveAirport) { + this.arriveAirport = arriveAirport; + } + + public Date getDepartureTime() { + return departureTime; + } + + public void setDepartureTime(Date departureTime) { + this.departureTime = departureTime; + } + + public Date getArriveTime() { + return arriveTime; + } + + public void setArriveTime(Date arriveTime) { + this.arriveTime = arriveTime; + } + + public String getModel() { + return model; + } + + public void setModel(String model) { + this.model = model; + } + + public Integer getIsDelete() { + return isDelete; + } + + public void setIsDelete(Integer isDelete) { + this.isDelete = isDelete; + } +} diff --git a/src/com/landaiqing/service/FlightService.java b/src/com/landaiqing/service/FlightService.java new file mode 100644 index 0000000..ac4cfa9 --- /dev/null +++ b/src/com/landaiqing/service/FlightService.java @@ -0,0 +1,29 @@ +package com.landaiqing.service; + +import com.landaiqing.dao.FlightDao; +import com.landaiqing.entity.FlightEntity; + +import java.util.List; + +public class FlightService { + private FlightDao flightDao= new FlightDao(); + public List getByAll() { + return flightDao.getByAll(); + } + + public int deleteFlghtById(Integer id) { + return flightDao.deleteFlghtById(id); + } + public FlightEntity getByIdFlight(Integer id) { + return flightDao.getByIdFlight(id); + } + public int updateFlight(FlightEntity flightEntity){ + return flightDao.updateFlight(flightEntity); + } + public int insertFlight(FlightEntity flightEntity){ + return flightDao.insertFlight(flightEntity); + } + public int updateDelete(Integer id){ + return flightDao.updateDelete(id); + } +} diff --git a/src/com/landaiqing/servlet/DeleteFlightServlet.java b/src/com/landaiqing/servlet/DeleteFlightServlet.java new file mode 100644 index 0000000..28ddb3f --- /dev/null +++ b/src/com/landaiqing/servlet/DeleteFlightServlet.java @@ -0,0 +1,45 @@ +package com.landaiqing.servlet; + +import com.landaiqing.service.FlightService; +import jakarta.servlet.ServletException; +import jakarta.servlet.annotation.WebServlet; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; + +import java.io.IOException; + +@WebServlet("/deleteFlight") +public class DeleteFlightServlet extends HttpServlet { + private FlightService flightService= new FlightService(); + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + String idStr = req.getParameter("id"); + if (idStr==null || idStr==""){ + req.setAttribute("errorMsg","ID的值不能为空!!!"); + req.getRequestDispatcher("error.jsp").forward(req,resp); + return; + } + + try { + Integer id = Integer.parseInt(idStr); + int result = flightService.updateDelete(id); + if (result > 0) { +// req.getRequestDispatcher("showFlight.jsp").forward(req,resp); + resp.sendRedirect("show.jsp"); + + }else { + req.setAttribute("errorMsg","删除失败!!!"); + req.getRequestDispatcher("error.jsp").forward(req,resp); + } + } catch (NumberFormatException e) { + req.setAttribute("errorMsg","类型转换异常,id 不能转换成Int类型!!!"); + req.getRequestDispatcher("error.jsp").forward(req,resp); + e.printStackTrace(); + }catch (Exception e){ + req.setAttribute("errorMsg","系统异常!!!"); + req.getRequestDispatcher("error.jsp").forward(req,resp); + } + } +} diff --git a/src/com/landaiqing/servlet/InsertServlet.java b/src/com/landaiqing/servlet/InsertServlet.java new file mode 100644 index 0000000..9cd575d --- /dev/null +++ b/src/com/landaiqing/servlet/InsertServlet.java @@ -0,0 +1,98 @@ +package com.landaiqing.servlet; + +import com.landaiqing.entity.FlightEntity; +import com.landaiqing.service.FlightService; +import com.landaiqing.utils.DateUtils; +import com.mysql.cj.util.StringUtils; +import jakarta.servlet.ServletException; +import jakarta.servlet.annotation.WebServlet; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; + +import java.io.IOException; +import java.util.Date; + +@WebServlet("/insert") +public class InsertServlet extends HttpServlet { + private FlightService flightService= new FlightService(); + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + req.getRequestDispatcher("insertFlight.jsp").forward(req,resp); + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + try { + String flightId = req.getParameter("flightId"); + if (StringUtils.isNullOrEmpty(flightId)){ + req.setAttribute("errorMsg","flightId 的值不能为空!!!"); + req.getRequestDispatcher("error.jsp").forward(req,resp); + return; + } + String company = req.getParameter("company"); + if (StringUtils.isNullOrEmpty(company)){ + req.setAttribute("errorMsg","company 的值不能为空!!!"); + req.getRequestDispatcher("error.jsp").forward(req,resp); + return; + } + String departureAirport = req.getParameter("departureAirport"); + if (StringUtils.isNullOrEmpty(departureAirport)){ + req.setAttribute("errorMsg","departureAirport 的值不能为空!!!"); + req.getRequestDispatcher("error.jsp").forward(req,resp); + return; + } + String arriveAirport = req.getParameter("arriveAirport"); + if (StringUtils.isNullOrEmpty(arriveAirport)){ + req.setAttribute("errorMsg","arriveAirport 的值不能为空!!!"); + req.getRequestDispatcher("error.jsp").forward(req,resp); + return; + } + String departureTimeStr = req.getParameter("departureTime"); + if (StringUtils.isNullOrEmpty(departureTimeStr)){ + req.setAttribute("errorMsg","departureTimeStr 的值不能为空!!!"); + req.getRequestDispatcher("error.jsp").forward(req,resp); + return; + } + + Date departureTime = DateUtils.stringToDate(departureTimeStr); + + String arriveTimeStr = req.getParameter("arriveTime"); + if (StringUtils.isNullOrEmpty(arriveTimeStr)){ + req.setAttribute("errorMsg","arriveTimeStr 的值不能为空!!!"); + req.getRequestDispatcher("error.jsp").forward(req,resp); + return; + } + + Date arriveTime = DateUtils.stringToDate(arriveTimeStr); + + String model = req.getParameter("model"); + if (StringUtils.isNullOrEmpty(model)){ + req.setAttribute("errorMsg","model 的值不能为空!!!"); + req.getRequestDispatcher("error.jsp").forward(req,resp); + return; + } + + FlightEntity flightEntity=new FlightEntity(); + flightEntity.setFlightId(flightId); + flightEntity.setDepartureAirport(departureAirport); + flightEntity.setArriveAirport(arriveAirport); + flightEntity.setDepartureTime(departureTime); + flightEntity.setArriveTime(arriveTime); + flightEntity.setModel(model); + flightEntity.setCompany(company); + int result = flightService.insertFlight(flightEntity); + if (result<=0){ + req.setAttribute("errorMsg","插入失败!!!"); + req.getRequestDispatcher("error.jsp").forward(req,resp); + return; + } + resp.sendRedirect("showFlight"); + } catch (Exception e) { + req.setAttribute("errorMsg","系统异常!!!"); + req.getRequestDispatcher("error.jsp").forward(req,resp); + throw new RuntimeException(e); + } + } +} diff --git a/src/com/landaiqing/servlet/ShowFlightServlet.java b/src/com/landaiqing/servlet/ShowFlightServlet.java new file mode 100644 index 0000000..9be5c08 --- /dev/null +++ b/src/com/landaiqing/servlet/ShowFlightServlet.java @@ -0,0 +1,29 @@ +package com.landaiqing.servlet; + +import com.landaiqing.entity.FlightEntity; +import com.landaiqing.service.FlightService; +import jakarta.servlet.ServletException; +import jakarta.servlet.annotation.WebServlet; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; + +import java.io.IOException; +import java.util.List; + +@WebServlet("/showFlight") +public class ShowFlightServlet extends HttpServlet { + private FlightService flightService= new FlightService(); + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + List flightEntities = flightService.getByAll(); + req.setAttribute("flights",flightEntities); + req.getRequestDispatcher("show.jsp").forward(req,resp); + + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + + } +} diff --git a/src/com/landaiqing/servlet/UpdateFlightServlet.java b/src/com/landaiqing/servlet/UpdateFlightServlet.java new file mode 100644 index 0000000..4832558 --- /dev/null +++ b/src/com/landaiqing/servlet/UpdateFlightServlet.java @@ -0,0 +1,125 @@ +package com.landaiqing.servlet; + +import com.landaiqing.entity.FlightEntity; +import com.landaiqing.service.FlightService; +import com.landaiqing.utils.DateUtils; +import com.mysql.cj.util.StringUtils; +import jakarta.servlet.ServletException; +import jakarta.servlet.annotation.WebServlet; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; + +import java.io.IOException; +import java.util.Date; + +@WebServlet("/update") +public class UpdateFlightServlet extends HttpServlet { + private FlightService flightService= new FlightService(); + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + String idStr = req.getParameter("id"); + if (idStr==null || idStr==""){ + req.setAttribute("errorMsg","ID的值不能为空!!!"); + req.getRequestDispatcher("error.jsp").forward(req,resp); + return; + } + try { + Integer id = Integer.parseInt(idStr); + FlightEntity flightEntity = flightService.getByIdFlight(id); + if (flightEntity==null){ + req.setAttribute("errorMsg","id不存在!!!"); + req.getRequestDispatcher("error.jsp").forward(req,resp); + return; + } + req.setAttribute("flight",flightEntity); + req.getRequestDispatcher("updateFlight.jsp").forward(req,resp); + }catch (Exception e){ + req.setAttribute("errorMsg","系统异常!!!"); + req.getRequestDispatcher("error.jsp").forward(req,resp); + } + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + try { + String idStr = req.getParameter("id"); + if (StringUtils.isNullOrEmpty(idStr)){ + req.setAttribute("errorMsg","id的值不能为空!!!"); + req.getRequestDispatcher("error.jsp").forward(req,resp); + return; + } + Integer id = Integer.parseInt(idStr); + String flightId = req.getParameter("flightId"); + if (StringUtils.isNullOrEmpty(flightId)){ + req.setAttribute("errorMsg","flightId 的值不能为空!!!"); + req.getRequestDispatcher("error.jsp").forward(req,resp); + return; + } + String company = req.getParameter("company"); + if (StringUtils.isNullOrEmpty(company)){ + req.setAttribute("errorMsg","company 的值不能为空!!!"); + req.getRequestDispatcher("error.jsp").forward(req,resp); + return; + } + String departureAirport = req.getParameter("departureAirport"); + if (StringUtils.isNullOrEmpty(departureAirport)){ + req.setAttribute("errorMsg","departureAirport 的值不能为空!!!"); + req.getRequestDispatcher("error.jsp").forward(req,resp); + return; + } + String arriveAirport = req.getParameter("arriveAirport"); + if (StringUtils.isNullOrEmpty(arriveAirport)){ + req.setAttribute("errorMsg","arriveAirport 的值不能为空!!!"); + req.getRequestDispatcher("error.jsp").forward(req,resp); + return; + } + String departureTimeStr = req.getParameter("departureTime"); + if (StringUtils.isNullOrEmpty(departureTimeStr)){ + req.setAttribute("errorMsg","departureTimeStr 的值不能为空!!!"); + req.getRequestDispatcher("error.jsp").forward(req,resp); + return; + } + + Date departureTime = DateUtils.stringToDate(departureTimeStr); + + String arriveTimeStr = req.getParameter("arriveTime"); + if (StringUtils.isNullOrEmpty(arriveTimeStr)){ + req.setAttribute("errorMsg","arriveTimeStr 的值不能为空!!!"); + req.getRequestDispatcher("error.jsp").forward(req,resp); + return; + } + + Date arriveTime = DateUtils.stringToDate(arriveTimeStr); + + String model = req.getParameter("model"); + if (StringUtils.isNullOrEmpty(model)){ + req.setAttribute("errorMsg","model 的值不能为空!!!"); + req.getRequestDispatcher("error.jsp").forward(req,resp); + return; + } + + FlightEntity flightEntity=new FlightEntity(); + flightEntity.setId(id); + flightEntity.setFlightId(flightId); + flightEntity.setDepartureAirport(departureAirport); + flightEntity.setArriveAirport(arriveAirport); + flightEntity.setDepartureTime(departureTime); + flightEntity.setArriveTime(arriveTime); + flightEntity.setModel(model); + flightEntity.setCompany(company); + int result = flightService.updateFlight(flightEntity); + if (result<=0){ + req.setAttribute("errorMsg","修改失败!!!"); + req.getRequestDispatcher("error.jsp").forward(req,resp); + return; + } + resp.sendRedirect("showFlight"); + } catch (Exception e) { + req.setAttribute("errorMsg","系统异常!!!"); + req.getRequestDispatcher("error.jsp").forward(req,resp); + throw new RuntimeException(e); + } + } +} diff --git a/src/com/landaiqing/test/Test01.java b/src/com/landaiqing/test/Test01.java new file mode 100644 index 0000000..4f72fbb --- /dev/null +++ b/src/com/landaiqing/test/Test01.java @@ -0,0 +1,23 @@ +package com.landaiqing.test; + +import com.landaiqing.dao.FlightDao; +import com.landaiqing.entity.FlightEntity; + +import java.util.Date; + +public class Test01 { + public static void main(String[] args) { + FlightDao flightDao=new FlightDao(); + FlightEntity flightEntity=new FlightEntity(); + flightEntity.setId(3); + flightEntity.setFlightId("002"); + flightEntity.setDepartureAirport("乌鲁木齐"); + flightEntity.setArriveAirport("陕西"); + flightEntity.setDepartureTime(new Date()); + flightEntity.setArriveTime(new Date()); + flightEntity.setModel("SSS"); + flightEntity.setCompany("中国"); + flightDao.updateFlight(flightEntity); + + } +} diff --git a/src/com/landaiqing/utils/DateUtils.java b/src/com/landaiqing/utils/DateUtils.java new file mode 100644 index 0000000..5f765ca --- /dev/null +++ b/src/com/landaiqing/utils/DateUtils.java @@ -0,0 +1,486 @@ +package com.landaiqing.utils; + +import java.sql.Timestamp; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.HashMap; +import java.util.Map; + + +public class DateUtils { + /** + * 预定的日期格式 + */ + public static final String[] DATEFORMAT = {"yyyy-MM-dd HH:mm:ss", "yyyy/MM/dd HH:mm:ss", "yyyy年MM月dd日HH时mm分ss秒", "yyyy-MM-dd", "yyyy/MM/dd", "yy-MM-dd", "yy/MM/dd", "yyyy年MM月dd日", "HH:mm:ss", + "yyyyMMddHHmmss", "yyyyMMdd", "yyyy.MM.dd", "yy.MM.dd", "yyyyMMddHHmmssSSS", "yyyy-MM-dd HH:mm:ss:SSS", "yyyy-MM-dd HH:mm:ss.SSS", "yyyy", "yyyyMM", "yyyyMMdd HH", "yyyyMMdd HH:mm", + "yyyyMMdd HH:mm:ss", "yyyy-MM" }; + + /** + * 线程绑定的日期格式转换器缓存 + */ + private static final ThreadLocal> DATEFORMATERSCACHE = new ThreadLocal>(); + + /** + * 获取当前系统时间 + */ + public static Calendar getSystemCurrentTime() { + final Calendar currentTime = Calendar.getInstance(); + return currentTime; + } + + /** + * 获取当前系统时间 + */ + public static String getSystemCurrentTime(int format) { + return toDateStrByFormatIndex(getSystemCurrentTime(), format); + } + + /** + * 获取系统当前date类型时间 + */ + public static Date getCurrentDate() { + return new Date(); + } + + /** + * 获取系统当前日期和时间,格式为yyyy-MM-dd HH:mm:ss + */ + public static String getCurrentDateTime() { + return getFormatCurrentDate("yyyy-MM-dd HH:mm:ss"); + } + + /** + * 返回格式化的当前日期/时间 + */ + public static String getFormatCurrentDate(String strFormat) { + return msFormatDateTime(getCurrentDate(), strFormat); + } + + /** + * 日期/时间格式化显示(年、月、日、时、分、秒、毫秒、星期) + */ + public static String msFormatDateTime(Date dtmDate, String strFormat) { + + if (strFormat.equals("")) { + strFormat = "yyyy-MM-dd HH:mm:ss"; + } + + final SimpleDateFormat myFormat = new SimpleDateFormat(strFormat); + + return myFormat.format(dtmDate.getTime()); + } + + /** + * 日期/时间格式化显示(年、月、日) + */ + public static String msFormatDate(Date dtmDate, String strFormat) { + + if (strFormat.equals("")) { + strFormat = "yyyy-MM-dd"; + } + + final SimpleDateFormat myFormat = new SimpleDateFormat(strFormat); + + return myFormat.format(dtmDate.getTime()); + } + + /** + * 获取当前系统时间 + */ + public static String getSystemCurrentTime(String format) { + return toDateStrByFormat(getSystemCurrentTime(), format); + } + + // ======================日期转字符串基础格式化方法====================================================================================== + + /** + * @name 中文名称 + */ + private static SimpleDateFormat getDateFormater(String format) { + Map dateFormaters = DATEFORMATERSCACHE.get(); + SimpleDateFormat dateFormater = null; + boolean formatersIsNull = false; + + if (dateFormaters == null) { + dateFormaters = new HashMap(3, 0.2f); + DATEFORMATERSCACHE.set(dateFormaters); + formatersIsNull = true; + } + dateFormater = dateFormaters.get(format); + if (formatersIsNull || dateFormater == null) { + dateFormater = new SimpleDateFormat(format); + dateFormaters.put(format, dateFormater); + } + + return dateFormater; + } + + + private static SimpleDateFormat getDateFormater(int format) { + return getDateFormater(DATEFORMAT[format]); + } + + // ======================日期转字符串基础方法====================================================================================== + + /** + * + * Calendar日期转指定日期格式的字符串 + */ + public static String toDateStrByFormat(Calendar date, String format) { + if (date == null) { + return null; + } + return getDateFormater(format).format(date.getTime()); + } + + /** + * + * Calendar日期转指定日期格式的字符串 + */ + public static String toDateStrByFormatIndex(Calendar date, int format) { + return toDateStrByFormat(date, DATEFORMAT[format]); + } + + /** + * java.util.Date日期转指定日期格式的字符串 + */ + public static String toDateStrByFormat(Date date, String format) { + if (date == null) { + return null; + } + return getDateFormater(format).format(date.getTime()); + } + + /** + * java.util.Date日期转指定格式的字符串 + */ + public static String toDateStrByFormatIndex(Date date, int format) { + return toDateStrByFormat(date, DATEFORMAT[format]); + } + + // ======================日期转字符串方法====================================================================================== + + /** + * Calendar日期转日期字符串 + */ + public static String toDateTimeStr(Calendar date) { + return toDateStrByFormatIndex(date, 0); + } + + /** + * Calendar日期转指定日期格式的字符串 + */ + public static String toDateTimeStr(int format, Calendar date) { + return toDateStrByFormatIndex(date, format); + } + + /** + * Calendar日期转日期字符串 + */ + public static String toDateStr(Calendar date) { + return toDateStrByFormatIndex(date, 3); + } + + /** + * java.util.Date日期转指定格式的日期字符串 + */ + public static String dateToString(Date date, int format) { + return toDateStrByFormatIndex(date, format); + } + + /** + * java.util.Date日期转日期字符串 + */ + public static String dateToString(Date date) { + return toDateStrByFormatIndex(date, 3); + } + + // ======================xx转Date方法====================================================================================== + + /** + * Calendar日期转java.util.Date日期 + */ + public static Date convertCalendarToDate(Calendar c) { + final Date d = new Date(); + d.setTime(c.getTimeInMillis()); + return d; + } + + /** + * 日期字符串转java.util.Date日期 + */ + public static Date stringToDate(String dateStr) throws Exception { + return parseDate(dateStr, 3); + } + + /** + * 日期字符串转指定格式的java.util.Date日期 + */ + public static Date parseDate(String dateStr, int format) throws Exception { + if (dateStr == null || dateStr.length() == 0) { + return null; + } + + try { + return getDateFormater(format).parse(dateStr); + } catch (ParseException ex) { + return parseDate(dateStr, format + 1); + } catch (ArrayIndexOutOfBoundsException ex) { + throw new Exception("UT-07001:日志字符串" + dateStr + "格式不支持", ex); + } + } + + // ======================xx转Calendar方法====================================================================================== + + /** + * java.util.Date转Calendar + */ + public static Calendar convUtilDateToUtilCalendar(Date date) { + if (date == null) { + return null; + } + + final GregorianCalendar gc = new GregorianCalendar(); + gc.setTimeInMillis(date.getTime()); + + return gc; + } + + /** + * java.sql.Timestamp转Calendar + */ + public static Calendar convSqlTimestampToUtilCalendar(Timestamp date) { + if (date == null) { + return null; + } + final GregorianCalendar gc = new GregorianCalendar(); + gc.setTimeInMillis(date.getTime()); + return gc; + } + + /** + * 日期字符串转Calendar + */ + public static Calendar parseDate(String dateStr) throws Exception { + final Date result = parseDate(dateStr, 0); + Calendar cal = null; + + if (result != null) { + cal = new GregorianCalendar(0, 0, 0, 0, 0, 0); + cal.setTime(result); + } + + return cal; + } + + // ======================日期转Timestamp方法====================================================================================== + + /** + * java.util.Date转java.sql.Timestamp + */ + public static Timestamp convUtilDateToSqlTimestamp(Date date) { + if (date == null) { + return null; + } + return new Timestamp(date.getTime()); + } + + /** + * Calendar日期对象转Timestamp日期对象 + */ + public static Timestamp convUtilCalendarToSqlTimestamp(Calendar date) { + if (date == null) { + return null; + } + return new Timestamp(date.getTimeInMillis()); + } + + /** + * Calendar日期对象转Timestamp日期对象 + */ + public static Timestamp parseTimestamp(Calendar calendar) { + return new Timestamp(calendar.getTimeInMillis()); + } + + /** + * 日期字符串转java.sql.Timestamp + */ + public static Timestamp parseTimestamp(String dateStr) throws Exception { + try { + return new Timestamp(getDateFormater(3).parse(dateStr).getTime()); + } catch (ParseException ex) { + throw new Exception("UT-07001:日志字符串" + dateStr + "格式不正确,格式:" + DATEFORMAT[3], ex); + } + } + + /** + * 根据指定日期格式,日期字符串转java.sql.Timestamp + */ + public static Timestamp parseTimestamp(String dateStr, int format) throws Exception { + try { + return new Timestamp(getDateFormater(format).parse(dateStr).getTime()); + } catch (ParseException ex) { + throw new Exception("UT-07001:日志字符串" + dateStr + "格式不支持", ex); + } + } + + // ======================日期计算方法====================================================================================== + + /** + * 获取两个Calendar日期对象的天数差 + */ + public static int calendarMinus(Calendar d1, Calendar d2) { + if (d1 == null || d2 == null) { + return 0; + } + + d1.set(11, 0); + d1.set(12, 0); + d1.set(13, 0); + d1.set(14, 0); + + d2.set(11, 0); + d2.set(12, 0); + d2.set(13, 0); + d2.set(14, 0); + + long t1 = d1.getTimeInMillis(); + long t2 = d2.getTimeInMillis(); + final long daylong = 86400000L; + t1 -= t1 % daylong; + t2 -= t2 % daylong; + + final long t = t1 - t2; + final int value = (int) (t / daylong); + + return value; + } + + /** + * 获取两个Calendar日期对象的天数差 + */ + public static long calendarminus(Calendar d1, Calendar d2) { + if (d1 == null || d2 == null) { + return 0L; + } + return (d1.getTimeInMillis() - d2.getTimeInMillis()) / 86400000L; + } + + /** + * 给定任意日期Calendar对象,计算所在月天数 + */ + public static int calcMonthDays(Calendar date) { + final Calendar t1 = (Calendar) date.clone(); + final Calendar t2 = (Calendar) date.clone(); + final int year = date.get(1); + final int month = date.get(2); + t1.set(year, month, 1); + t2.set(year, month + 1, 1); + t2.add(6, -1); + return calendarMinus(t2, t1) + 1; + } + + private static int calcDays(long t1, long t2) { + final long millis = t1 - t2; + if (millis == 0) { + return 0; + } + return (int) (millis / (24 * 3600 * 1000)); + } + + /** + * 获取两个Calendar日期对象的天数差 + */ + public static int calcDays(Calendar c1, Calendar c2) { + return calcDays(c1.getTimeInMillis(), c2.getTimeInMillis()); + } + + /** + * 获取两个java.util.Date日期对象的天数差 + */ + public static int calcDays(Date d1, Date d2) { + return calcDays(d1.getTime(), d2.getTime()); + } + + /** + * 给定任意日期Calendar对象,计算所在月的最后一天 + */ + public static Calendar lastDay(Calendar c) { + final Calendar t = (Calendar) c.clone(); + t.set(Calendar.DAY_OF_MONTH, 1); + t.add(Calendar.MONTH, 1); + t.add(Calendar.DAY_OF_MONTH, -1); + return t; + } + + /** + * 给定任意日期字符串,计算所在月的最后一天 + */ + public static Calendar lastDay(String dateStr) throws Exception { + final Calendar t = parseDate(dateStr); + t.set(Calendar.DAY_OF_MONTH, 1); + t.add(Calendar.MONTH, 1); + t.add(Calendar.DAY_OF_MONTH, -1); + return t; + + } + + /** + * 给定任意日期,计算所在季的季起日期和季终日期 + */ + public static Calendar[] calcAQuarter(Calendar day) { + final Calendar[] quarter = new Calendar[2]; + + int month = 0; + quarter[0] = (Calendar) day.clone(); + month = quarter[0].get(Calendar.MONTH); + + if (month < 3) { + month = 0; + } else if (month < 6) { + month = 3; + } else if (month < 9) { + month = 6; + } else { + month = 9; + } + + quarter[0].set(Calendar.MONTH, month); + quarter[0].set(Calendar.DAY_OF_MONTH, 1); + + quarter[1] = (Calendar) quarter[0].clone(); + quarter[1].set(Calendar.MONTH, month + 2); + quarter[1] = lastDay(quarter[1]); + + return quarter; + } + + /** + * 获取年、月、日、时、分、秒、毫秒 + */ + public static int[] getYearMonthDayHH24MiMM(Calendar calendar) { + return new int[] {calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), + calendar.get(Calendar.SECOND), calendar.get(Calendar.MILLISECOND) }; + } + + /** + * 获取年、月、日、时、分、秒、毫秒 + */ + public static int[] getYearMonthDayHH24MiMM(Date date) { + final Calendar calendar = getSystemCurrentTime(); + calendar.setTime(date); + return getYearMonthDayHH24MiMM(calendar); + } + + /** + * 好微妙转毫秒 + */ + public static long nsToMs(long nsTime) { + return nsTime / 1000000; + } + +} + diff --git a/src/com/landaiqing/utils/JDBCUtil.java b/src/com/landaiqing/utils/JDBCUtil.java new file mode 100644 index 0000000..0ba73bc --- /dev/null +++ b/src/com/landaiqing/utils/JDBCUtil.java @@ -0,0 +1,140 @@ +package com.landaiqing.utils; + + +import java.io.InputStream; +import java.sql.*; +import java.util.Properties; + +/** + * @author 余胜军 + * @ClassName MayiktJdbcUtils + * @qq 644064779 + * @addres www.mayikt.com + * 微信:yushengjun644 + */ +public class JDBCUtil { + /** + * 1.需要将我们的构造方法私有化 ---工具类 不需要 new出来 是通过类名称.方法名称访问 + */ + private JDBCUtil() { + + } + + /** + * 2.定义工具类 需要 声明 变量 + */ + private static String driverClass; + private static String url; + private static String user; + private static String password; + + /** + *3.使用静态代码快 来给我们声明好 jdbc变量赋值(读取config.properties) + */ + static { + try { + // 1.读取config.properties IO 路径 相对路径 + InputStream resourceAsStream = JDBCUtil.class.getClassLoader(). + getResourceAsStream("config.properties"); + // 2.赋值给我们声明好的变量 + Properties properties = new Properties(); + properties.load(resourceAsStream); + driverClass = properties.getProperty("driverClass"); + url = properties.getProperty("url"); + user = properties.getProperty("user"); + password = properties.getProperty("password"); + // 3.注册驱动类 + Class.forName(driverClass); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * 4.封装连接方法 + */ + public static Connection getConnection() throws SQLException { + Connection connection = DriverManager.getConnection(url, user, password); + return connection; + } + + /** + * 5.封装释放连接方法 (重载) + */ + public static void closeConnection(ResultSet resultSet, Statement statement, Connection connection) { + // 1.查询 释放连接 resultSet statement connection + try { + if (resultSet != null) + resultSet.close(); + if (statement != null) + statement.close(); + if (connection != null) + connection.close(); + } catch (Exception e) { + e.printStackTrace(); + } + + } + + /** + * 增删改---释放jdbc资源 + * + * @param statement + * @param connection + */ + public static void closeConnection(Statement statement, Connection connection) { + // 1.查询 释放连接 resultSet statement connection + closeConnection(null, statement, connection); + } + + /** + * 开启事务 + * + * @param connection + * @throws SQLException + */ + public static void beginTransaction(Connection connection) throws SQLException { + connection.setAutoCommit(false); + } + + /** + * 提交事务 + * + * @param connection + * @throws SQLException + */ + public static void commitTransaction(Connection connection) throws SQLException { + connection.commit(); + } + + /** + * 回滚事务 + * + * @param connection + */ + public static void rollBackTransaction(Connection connection) { + if (connection != null) { + try { + connection.rollback(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + } + + /** + * 关闭事务 + * + * @param connection + */ + public static void endTransaction(Connection connection) { + if (connection != null) { + try { + connection.setAutoCommit(true); + } catch (SQLException e) { + e.printStackTrace(); + } + } + } + +} diff --git a/src/config.properties b/src/config.properties new file mode 100644 index 0000000..eb38c99 --- /dev/null +++ b/src/config.properties @@ -0,0 +1,4 @@ +driverClass=com.mysql.cj.jdbc.Driver +url=jdbc:mysql://127.0.0.1:3306/testdb?serverTimezone=GMT%2B8 +user=root +password=1611 \ No newline at end of file diff --git a/web/WEB-INF/web.xml b/web/WEB-INF/web.xml new file mode 100644 index 0000000..d80081d --- /dev/null +++ b/web/WEB-INF/web.xml @@ -0,0 +1,6 @@ + + + \ No newline at end of file diff --git a/web/error.jsp b/web/error.jsp new file mode 100644 index 0000000..f9edeb4 --- /dev/null +++ b/web/error.jsp @@ -0,0 +1,16 @@ +<%-- + Created by IntelliJ IDEA. + User: LDQ + Date: 2023/6/5 + Time: 19:06 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + ERROR + + +

系统错误,错误信息:${errorMsg}

+ + diff --git a/web/flight.html b/web/flight.html new file mode 100644 index 0000000..e057abe --- /dev/null +++ b/web/flight.html @@ -0,0 +1,34 @@ + + + + + Title + + + + + + + + + + + + + + + + + + + + + + + + + + +
航号航空公司出发机场达到机场出发时间到达时间机型操作
MYKT001中国东方航空武汉天河机场北京首都机场2022年5月25日 12:002022年5月25日 14:00735修改    删除
+ + \ No newline at end of file diff --git a/web/index.jsp b/web/index.jsp new file mode 100644 index 0000000..c9889ae --- /dev/null +++ b/web/index.jsp @@ -0,0 +1,16 @@ +<%-- + Created by IntelliJ IDEA. + User: LDQ + Date: 2023/6/5 + Time: 17:52 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + $Title$ + + + $END$ + + diff --git a/web/insertFlight.jsp b/web/insertFlight.jsp new file mode 100644 index 0000000..0094acb --- /dev/null +++ b/web/insertFlight.jsp @@ -0,0 +1,26 @@ +<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + + + 添加航班信息 + + +
+

添加数据

+ +
+
+
+
+
+
+
+
+ +
+
+ + + \ No newline at end of file diff --git a/web/show.jsp b/web/show.jsp new file mode 100644 index 0000000..d911dd5 --- /dev/null +++ b/web/show.jsp @@ -0,0 +1,46 @@ +<%-- + Created by IntelliJ IDEA. + User: LDQ + Date: 2023/6/5 + Time: 18:42 + To change this template use File | Settings | File Templates. +--%> +<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 航班系统 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 点击添加航班 + + +
序号航号航空公司出发机场达到机场出发时间到达时间机型操作
${i.index+1}${f.flightId}${f.company}${f.departureAirport}${f.arriveAirport}${f.departureTime}${f.arriveTime}${f.model}修改    删除
+ + diff --git a/web/updateFlight.html b/web/updateFlight.html new file mode 100644 index 0000000..012eb3f --- /dev/null +++ b/web/updateFlight.html @@ -0,0 +1,24 @@ + + + + + Title + + +
+

修改数据

+ +
+
+
+
+
+
+
+
+ +
+
+ + + \ No newline at end of file diff --git a/web/updateFlight.jsp b/web/updateFlight.jsp new file mode 100644 index 0000000..958662f --- /dev/null +++ b/web/updateFlight.jsp @@ -0,0 +1,27 @@ +<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + + + 修改航班信息 + + +
+

修改数据

+ +
+
+
+
+
+
+
+
+
+ +
+
+ + + \ No newline at end of file