根据文件步骤依据JDK环境下的JAVA环境为基础写出相应的JAVA代码
时间: 2024-12-28 08:40:45 浏览: 10
根据提供的文档内容,我将为你编写一个简单的Java类 `FlightQuery`,用于实现航班查询功能。这个类将连接到MySQL数据库,并根据用户输入的出发地、目的地和出发时间查询航班信息。
### 1. 导入必要的包
首先,我们需要导入一些必要的包:
```java
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;
```
### 2. 创建 `Flight` 类
定义一个 `Flight` 类来表示航班对象:
```java
public class Flight {
private String flightNumber;
private String airline;
private String departureAirport;
private String arrivalAirport;
private Date departureTime;
private Date arrivalTime;
private double fare;
private int remainingTickets;
// Getters and Setters
public String getFlightNumber() {
return flightNumber;
}
public void setFlightNumber(String flightNumber) {
this.flightNumber = flightNumber;
}
public String getAirline() {
return airline;
}
public void setAirline(String airline) {
this.airline = airline;
}
public String getDepartureAirport() {
return departureAirport;
}
public void setDepartureAirport(String departureAirport) {
this.departureAirport = departureAirport;
}
public String getArrivalAirport() {
return arrivalAirport;
}
public void setArrivalAirport(String arrivalAirport) {
this.arrivalAirport = arrivalAirport;
}
public Date getDepartureTime() {
return departureTime;
}
public void setDepartureTime(Date departureTime) {
this.departureTime = departureTime;
}
public Date getArrivalTime() {
return arrivalTime;
}
public void setArrivalTime(Date arrivalTime) {
this.arrivalTime = arrivalTime;
}
public double getFare() {
return fare;
}
public void setFare(double fare) {
this.fare = fare;
}
public int getRemainingTickets() {
return remainingTickets;
}
public void setRemainingTickets(int remainingTickets) {
this.remainingTickets = remainingTickets;
}
}
```
### 3. 创建 `FlightQuery` 类
定义一个 `FlightQuery` 类来实现航班查询功能:
```java
public class FlightQuery {
// 数据库连接信息
private static final String URL = "jdbc:mysql://localhost:3306/airline_system";
private static final String USER = "root";
private static final String PASSWORD = "password";
/**
* 根据出发地、目的地和出发时间查询航班信息
*
* @param departure 出发地
* @param destination 目的地
* @param departureTime 出发时间
* @return 符合条件的航班列表
*/
public List<Flight> queryFlights(String departure, String destination, Date departureTime) {
List<Flight> flights = new ArrayList<>();
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
try {
// 连接到数据库
connection = DriverManager.getConnection(URL, USER, PASSWORD);
// 编写SQL查询语句
String sql = "SELECT * FROM flight WHERE departure_airport = ? AND arrival_airport = ? AND departure_time >= ?";
statement = connection.prepareStatement(sql);
// 设置参数
statement.setString(1, departure);
statement.setString(2, destination);
statement.setDate(3, new java.sql.Date(departureTime.getTime()));
// 执行查询
resultSet = statement.executeQuery();
// 处理查询结果
while (resultSet.next()) {
Flight flight = new Flight();
flight.setFlightNumber(resultSet.getString("flight_number"));
flight.setAirline(resultSet.getString("airline"));
flight.setDepartureAirport(resultSet.getString("departure_airport"));
flight.setArrivalAirport(resultSet.getString("arrival_airport"));
flight.setDepartureTime(resultSet.getTimestamp("departure_time"));
flight.setArrivalTime(resultSet.getTimestamp("arrival_time"));
flight.setFare(resultSet.getDouble("fare"));
flight.setRemainingTickets(resultSet.getInt("remaining_tickets"));
flights.add(flight);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭资源
try {
if (resultSet != null) resultSet.close();
if (statement != null) statement.close();
if (connection != null) connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return flights;
}
}
```
### 4. 使用 `FlightQuery` 类
下面是一个简单的示例,演示如何使用 `FlightQuery` 类查询航班信息:
```java
public class Main {
public static void main(String[] args) {
FlightQuery flightQuery = new FlightQuery();
// 示例查询条件
String departure = "北京首都机场";
String destination = "上海浦东机场";
Date departureTime = new Date(System.currentTimeMillis());
// 执行查询
List<Flight> flights = flightQuery.queryFlights(departure, destination, departureTime);
// 输出查询结果
for (Flight flight : flights) {
System.out.println("航班号: " + flight.getFlightNumber());
System.out.println("航空公司: " + flight.getAirline());
System.out.println("出发机场: " + flight.getDepartureAirport());
System.out.println("到达机场: " + flight.getArrivalAirport());
System.out.println("出发时间: " + flight.getDepartureTime());
System.out.println("到达时间: " + flight.getArrivalTime());
System.out.println("票价: " + flight.getFare());
System.out.println("余票数量: " + flight.getRemainingTickets());
System.out.println("------------------------");
}
}
}
```
### 5. 注意事项
1. **数据库连接信息**:请确保 `URL`、`USER` 和 `PASSWORD` 是正确的,与你的数据库配置相匹配。
2. **日期格式**:确保传入的 `Date` 对象格式与数据库中的日期格式一致。
3. **异常处理**:在实际应用中,建议对异常进行更详细的处理,以提高系统的健壮性。
以上代码实现了一个基本的航班查询功能。你可以在此基础上扩展更多功能,如订票、支付等。
阅读全文