设计一个长途汽车信息管理系统,包含以下信息:线路信息:包括线路编号、出发地、目的地、出发时间、所需时间。汽车信息:包括汽车编号、汽车种类和对应的票价、最大的载客量。票价信息:包括车票编号、售票情况、票价、折扣。用户信息:包括用户编号、出发地、目的地、出发时间。查询打印相应的信息。同时使用数据库连接技术,在java里实现某一模块的增删改查功能,请提供设计思路和详细代码
时间: 2023-07-10 22:42:37 浏览: 133
好的,以下是我为您提供的设计思路和详细代码。
设计思路:
1. 数据库设计:根据要求,需要设计线路信息、汽车信息、票价信息和用户信息四个表。每个表中包含相关字段,例如线路信息表包括线路编号、出发地、目的地、出发时间、所需时间等字段。
2. 系统界面设计:根据要求,需要设计一个用户界面,包括查询界面和管理员界面。查询界面包括出发地、目的地、出发时间等查询条件,管理员界面包括添加、删除、修改和查询功能。
3. 后台代码实现:根据数据库设计和系统界面设计,需要编写后台代码实现系统功能。例如,添加线路信息的后台代码实现如下:
```
public void addRoute(String routeId, String startLocation, String endLocation, String departureTime, String duration) {
try {
Connection conn = DriverManager.getConnection(url, username, password);
String sql = "INSERT INTO routes (route_id, start_location, end_location, departure_time, duration) VALUES (?, ?, ?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, routeId);
pstmt.setString(2, startLocation);
pstmt.setString(3, endLocation);
pstmt.setString(4, departureTime);
pstmt.setString(5, duration);
pstmt.executeUpdate();
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
```
4. 前端代码实现:根据系统界面设计,需要编写前端代码实现系统交互。例如,查询界面的前端代码实现如下:
```
<form>
<label for="startLocation">出发地:</label>
<input type="text" id="startLocation" name="startLocation"><br><br>
<label for="endLocation">目的地:</label>
<input type="text" id="endLocation" name="endLocation"><br><br>
<label for="departureTime">出发时间:</label>
<input type="datetime-local" id="departureTime" name="departureTime"><br><br>
<input type="button" value="查询" onclick="query()">
</form>
```
详细代码实现:
1. 数据库设计
首先需要创建一个名为bus_info的数据库,然后创建四个表格:routes、cars、tickets、users。分别对应线路信息、汽车信息、票价信息和用户信息。
routes表格包含以下字段:route_id、start_location、end_location、departure_time、duration。
cars表格包含以下字段:car_id、car_type、price、capacity。
tickets表格包含以下字段:ticket_id、route_id、car_id、user_id、status、price、discount。
users表格包含以下字段:user_id、start_location、end_location、departure_time。
2. 后台代码实现
在Java中使用JDBC技术连接数据库,具体实现如下:
```
import java.sql.*;
public class BusInfoSystem {
private String url = "jdbc:mysql://localhost:3306/bus_info";
private String username = "root";
private String password = "";
// 添加线路信息
public void addRoute(String routeId, String startLocation, String endLocation, String departureTime, String duration) {
try {
Connection conn = DriverManager.getConnection(url, username, password);
String sql = "INSERT INTO routes (route_id, start_location, end_location, departure_time, duration) VALUES (?, ?, ?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, routeId);
pstmt.setString(2, startLocation);
pstmt.setString(3, endLocation);
pstmt.setString(4, departureTime);
pstmt.setString(5, duration);
pstmt.executeUpdate();
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 删除线路信息
public void deleteRoute(String routeId) {
try {
Connection conn = DriverManager.getConnection(url, username, password);
String sql = "DELETE FROM routes WHERE route_id=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, routeId);
pstmt.executeUpdate();
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 修改线路信息
public void updateRoute(String routeId, String startLocation, String endLocation, String departureTime, String duration) {
try {
Connection conn = DriverManager.getConnection(url, username, password);
String sql = "UPDATE routes SET start_location=?, end_location=?, departure_time=?, duration=? WHERE route_id=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, startLocation);
pstmt.setString(2, endLocation);
pstmt.setString(3, departureTime);
pstmt.setString(4, duration);
pstmt.setString(5, routeId);
pstmt.executeUpdate();
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 查询线路信息
public void queryRoute(String startLocation, String endLocation, String departureTime) {
try {
Connection conn = DriverManager.getConnection(url, username, password);
String sql = "SELECT * FROM routes WHERE start_location=? AND end_location=? AND departure_time=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, startLocation);
pstmt.setString(2, endLocation);
pstmt.setString(3, departureTime);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
System.out.println(rs.getString("route_id") + " " + rs.getString("start_location") + " " + rs.getString("end_location") + " " + rs.getString("departure_time") + " " + rs.getString("duration"));
}
rs.close();
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 添加汽车信息
public void addCar(String carId, String carType, String price, String capacity) {
try {
Connection conn = DriverManager.getConnection(url, username, password);
String sql = "INSERT INTO cars (car_id, car_type, price, capacity) VALUES (?, ?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, carId);
pstmt.setString(2, carType);
pstmt.setString(3, price);
pstmt.setString(4, capacity);
pstmt.executeUpdate();
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 删除汽车信息
public void deleteCar(String carId) {
try {
Connection conn = DriverManager.getConnection(url, username, password);
String sql = "DELETE FROM cars WHERE car_id=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, carId);
pstmt.executeUpdate();
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 修改汽车信息
public void updateCar(String carId, String carType, String price, String capacity) {
try {
Connection conn = DriverManager.getConnection(url, username, password);
String sql = "UPDATE cars SET car_type=?, price=?, capacity=? WHERE car_id=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, carType);
pstmt.setString(2, price);
pstmt.setString(3, capacity);
pstmt.setString(4, carId);
pstmt.executeUpdate();
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 查询汽车信息
public void queryCar(String carType) {
try {
Connection conn = DriverManager.getConnection(url, username, password);
String sql = "SELECT * FROM cars WHERE car_type=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, carType);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
System.out.println(rs.getString("car_id") + " " + rs.getString("car_type") + " " + rs.getString("price") + " " + rs.getString("capacity"));
}
rs.close();
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 添加票价信息
public void addTicket(String ticketId, String routeId, String carId, String userId, String status, String price, String discount) {
try {
Connection conn = DriverManager.getConnection(url, username, password);
String sql = "INSERT INTO tickets (ticket_id, route_id, car_id, user_id, status, price, discount) VALUES (?, ?, ?, ?, ?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, ticketId);
pstmt.setString(2, routeId);
pstmt.setString(3, carId);
pstmt.setString(4, userId);
pstmt.setString(5, status);
pstmt.setString(6, price);
pstmt.setString(7, discount);
pstmt.executeUpdate();
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 删除票价信息
public void deleteTicket(String ticketId) {
try {
Connection conn = DriverManager.getConnection(url, username, password);
String sql = "DELETE FROM tickets WHERE ticket_id=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, ticketId);
pstmt.executeUpdate();
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 修改票价信息
public void updateTicket(String ticketId, String status, String price, String discount) {
try {
Connection conn = DriverManager.getConnection(url, username, password);
String sql = "UPDATE tickets SET status=?, price=?, discount=? WHERE ticket_id=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, status);
pstmt.setString(2, price);
pstmt.setString(3, discount);
pstmt.setString(4, ticketId);
pstmt.executeUpdate();
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 查询票价信息
public void queryTicket(String userId) {
try {
Connection conn = DriverManager.getConnection(url, username, password);
String sql = "SELECT * FROM tickets WHERE user_id=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, userId);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
System.out.println(rs.getString("ticket_id") + " " + rs.getString("route_id") + " " + rs.getString("car_id") + " " + rs.getString("user_id") + " " + rs.getString("status") + " " + rs.getString("price") + " " + rs.getString("discount"));
}
rs.close();
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
阅读全文