航班信息管理系统java的代码
时间: 2024-03-13 08:46:03 浏览: 16
抱歉,由于航班信息管理系统是一个较为复杂的系统,其代码量较大,我无法在这里提供完整的代码。但是,我可以向您介绍一些该系统中可能会用到的Java代码示例,以供参考。
1. 连接数据库
```java
import java.sql.*;
public class DBUtil {
private static final String URL = "jdbc:mysql://localhost:3306/flight_info";
private static final String USERNAME = "root";
private static final String PASSWORD = "password";
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void close(Connection conn, Statement stmt, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
2. 查询航班信息
```java
import java.sql.*;
public class FlightInfoDAO {
public FlightInfo getFlightInfo(String flightNumber) {
FlightInfo flightInfo = null;
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = DBUtil.getConnection();
pstmt = conn.prepareStatement("SELECT * FROM flight_info WHERE flight_number = ?");
pstmt.setString(1, flightNumber);
rs = pstmt.executeQuery();
if (rs.next()) {
flightInfo = new FlightInfo();
flightInfo.setFlightNumber(rs.getString("flight_number"));
flightInfo.setDeparture(rs.getString("departure"));
flightInfo.setDestination(rs.getString("destination"));
flightInfo.setDepartureTime(rs.getTimestamp("departure_time"));
flightInfo.setArrivalTime(rs.getTimestamp("arrival_time"));
flightInfo.setPlaneModel(rs.getString("plane_model"));
flightInfo.setPrice(rs.getDouble("price"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, pstmt, rs);
}
return flightInfo;
}
}
```
3. 添加航班信息
```java
import java.sql.*;
public class FlightInfoDAO {
public boolean addFlightInfo(FlightInfo flightInfo) {
boolean success = false;
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = DBUtil.getConnection();
pstmt = conn.prepareStatement("INSERT INTO flight_info(flight_number, departure, destination, departure_time, arrival_time, plane_model, price) VALUES (?, ?, ?, ?, ?, ?, ?)");
pstmt.setString(1, flightInfo.getFlightNumber());
pstmt.setString(2, flightInfo.getDeparture());
pstmt.setString(3, flightInfo.getDestination());
pstmt.setTimestamp(4, new Timestamp(flightInfo.getDepartureTime().getTime()));
pstmt.setTimestamp(5, new Timestamp(flightInfo.getArrivalTime().getTime()));
pstmt.setString(6, flightInfo.getPlaneModel());
pstmt.setDouble(7, flightInfo.getPrice());
int rows = pstmt.executeUpdate();
if (rows > 0) {
success = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, pstmt, null);
}
return success;
}
}
```
4. 修改航班信息
```java
import java.sql.*;
public class FlightInfoDAO {
public boolean updateFlightInfo(FlightInfo flightInfo) {
boolean success = false;
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = DBUtil.getConnection();
pstmt = conn.prepareStatement("UPDATE flight_info SET departure = ?, destination = ?, departure_time = ?, arrival_time = ?, plane_model = ?, price = ? WHERE flight_number = ?");
pstmt.setString(1, flightInfo.getDeparture());
pstmt.setString(2, flightInfo.getDestination());
pstmt.setTimestamp(3, new Timestamp(flightInfo.getDepartureTime().getTime()));
pstmt.setTimestamp(4, new Timestamp(flightInfo.getArrivalTime().getTime()));
pstmt.setString(5, flightInfo.getPlaneModel());
pstmt.setDouble(6, flightInfo.getPrice());
pstmt.setString(7, flightInfo.getFlightNumber());
int rows = pstmt.executeUpdate();
if (rows > 0) {
success = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, pstmt, null);
}
return success;
}
}
```
以上是一些可能用到的Java代码示例,仅供参考。实际的开发过程中,需要根据具体的需求进行设计和实现。