175 lines
7.8 KiB
Java
175 lines
7.8 KiB
Java
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<FlightEntity> 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<FlightEntity> 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);
|
|
}
|
|
}
|
|
}
|