用java和数据库编写一个车辆预约系统的代码
时间: 2024-03-05 19:54:08 浏览: 57
很抱歉,由于篇幅限制,无法在此提供完整的代码。但是,我可以为您提供一个简单的车辆预约系统的代码框架,帮助您更好地理解系统的实现方式。
1. 数据库连接类:
```java
import java.sql.*;
public class DBUtil {
private static final String URL = "jdbc:mysql://localhost:3306/reservation?useSSL=false&serverTimezone=UTC";
private static final String USER = "root";
private static final String PASSWORD = "123456";
static {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(URL, USER, PASSWORD);
}
public static void close(Connection conn, Statement st, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
if (st != null) {
st.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
2. 用户管理类:
```java
import java.sql.*;
public class UserManager {
public boolean addUser(User user) {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = DBUtil.getConnection();
String sql = "INSERT INTO user(name, phone, email, password) VALUES (?, ?, ?, ?)";
ps = conn.prepareStatement(sql);
ps.setString(1, user.getName());
ps.setString(2, user.getPhone());
ps.setString(3, user.getEmail());
ps.setString(4, user.getPassword());
int count = ps.executeUpdate();
return count > 0;
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, ps, null);
}
return false;
}
public boolean updateUser(User user) {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = DBUtil.getConnection();
String sql = "UPDATE user SET name=?, phone=?, email=?, password=? WHERE id=?";
ps = conn.prepareStatement(sql);
ps.setString(1, user.getName());
ps.setString(2, user.getPhone());
ps.setString(3, user.getEmail());
ps.setString(4, user.getPassword());
ps.setInt(5, user.getId());
int count = ps.executeUpdate();
return count > 0;
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, ps, null);
}
return false;
}
public User getUserById(int id) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = DBUtil.getConnection();
String sql = "SELECT * FROM user WHERE id=?";
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
rs = ps.executeQuery();
if (rs.next()) {
User user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setPhone(rs.getString("phone"));
user.setEmail(rs.getString("email"));
user.setPassword(rs.getString("password"));
return user;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, ps, rs);
}
return null;
}
public User getUserByEmail(String email) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = DBUtil.getConnection();
String sql = "SELECT * FROM user WHERE email=?";
ps = conn.prepareStatement(sql);
ps.setString(1, email);
rs = ps.executeQuery();
if (rs.next()) {
User user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setPhone(rs.getString("phone"));
user.setEmail(rs.getString("email"));
user.setPassword(rs.getString("password"));
return user;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, ps, rs);
}
return null;
}
}
```
3. 车辆管理类:
```java
import java.sql.*;
public class VehicleManager {
public boolean addVehicle(Vehicle vehicle) {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = DBUtil.getConnection();
String sql = "INSERT INTO vehicle(brand, model, license_plate, color, status) VALUES (?, ?, ?, ?, ?)";
ps = conn.prepareStatement(sql);
ps.setString(1, vehicle.getBrand());
ps.setString(2, vehicle.getModel());
ps.setString(3, vehicle.getLicensePlate());
ps.setString(4, vehicle.getColor());
ps.setInt(5, vehicle.getStatus());
int count = ps.executeUpdate();
return count > 0;
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, ps, null);
}
return false;
}
public boolean updateVehicle(Vehicle vehicle) {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = DBUtil.getConnection();
String sql = "UPDATE vehicle SET brand=?, model=?, license_plate=?, color=?, status=? WHERE id=?";
ps = conn.prepareStatement(sql);
ps.setString(1, vehicle.getBrand());
ps.setString(2, vehicle.getModel());
ps.setString(3, vehicle.getLicensePlate());
ps.setString(4, vehicle.getColor());
ps.setInt(5, vehicle.getStatus());
ps.setInt(6, vehicle.getId());
int count = ps.executeUpdate();
return count > 0;
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, ps, null);
}
return false;
}
public Vehicle getVehicleById(int id) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = DBUtil.getConnection();
String sql = "SELECT * FROM vehicle WHERE id=?";
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
rs = ps.executeQuery();
if (rs.next()) {
Vehicle vehicle = new Vehicle();
vehicle.setId(rs.getInt("id"));
vehicle.setBrand(rs.getString("brand"));
vehicle.setModel(rs.getString("model"));
vehicle.setLicensePlate(rs.getString("license_plate"));
vehicle.setColor(rs.getString("color"));
vehicle.setStatus(rs.getInt("status"));
return vehicle;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, ps, rs);
}
return null;
}
public List<Vehicle> getAvailableVehicles() {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = DBUtil.getConnection();
String sql = "SELECT * FROM vehicle WHERE status=0";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
List<Vehicle> list = new ArrayList<>();
while (rs.next()) {
Vehicle vehicle = new Vehicle();
vehicle.setId(rs.getInt("id"));
vehicle.setBrand(rs.getString("brand"));
vehicle.setModel(rs.getString("model"));
vehicle.setLicensePlate(rs.getString("license_plate"));
vehicle.setColor(rs.getString("color"));
vehicle.setStatus(rs.getInt("status"));
list.add(vehicle);
}
return list;
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, ps, rs);
}
return null;
}
}
```
4. 预约管理类:
```java
import java.sql.*;
import java.util.Date;
public class ReservationManager {
public boolean addReservation(Reservation reservation) {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = DBUtil.getConnection();
String sql = "INSERT INTO reservation(user_id, vehicle_id, start_time, end_time, status) VALUES (?, ?, ?, ?, ?)";
ps = conn.prepareStatement(sql);
ps.setInt(1, reservation.getUserId());
ps.setInt(2, reservation.getVehicleId());
ps.setTimestamp(3, new Timestamp(reservation.getStartTime().getTime()));
ps.setTimestamp(4, new Timestamp(reservation.getEndTime().getTime()));
ps.setInt(5, reservation.getStatus());
int count = ps.executeUpdate();
return count > 0;
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, ps, null);
}
return false;
}
public boolean updateReservation(Reservation reservation) {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = DBUtil.getConnection();
String sql = "UPDATE reservation SET user_id=?, vehicle_id=?, start_time=?, end_time=?, status=? WHERE id=?";
ps = conn.prepareStatement(sql);
ps.setInt(1, reservation.getUserId());
ps.setInt(2, reservation.getVehicleId());
ps.setTimestamp(3, new Timestamp(reservation.getStartTime().getTime()));
ps.setTimestamp(4, new Timestamp(reservation.getEndTime().getTime()));
ps.setInt(5, reservation.getStatus());
ps.setInt(6, reservation.getId());
int count = ps.executeUpdate();
return count > 0;
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, ps, null);
}
return false;
}
public Reservation getReservationById(int id) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = DBUtil.getConnection();
String sql = "SELECT * FROM reservation WHERE id=?";
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
rs = ps.executeQuery();
if (rs.next()) {
Reservation reservation = new Reservation();
reservation.setId(rs.getInt("id"));
reservation.setUserId(rs.getInt("user_id"));
reservation.setVehicleId(rs.getInt("vehicle_id"));
reservation.setStartTime(rs.getTimestamp("start_time"));
reservation.setEndTime(rs.getTimestamp("end_time"));
reservation.setStatus(rs.getInt("status"));
return reservation;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, ps, rs);
}
return null;
}
public List<Reservation> getReservationsByUserId(int userId) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = DBUtil.getConnection();
String sql = "SELECT * FROM reservation WHERE user_id=?";
ps = conn.prepareStatement(sql);
ps.setInt(1, userId);
rs = ps.executeQuery();
List<Reservation> list = new ArrayList<>();
while (rs.next()) {
Reservation reservation = new Reservation();
reservation.setId(rs.getInt("id"));
reservation.setUserId(rs.getInt("user_id"));
reservation.setVehicleId(rs.getInt("vehicle_id"));
reservation.setStartTime(rs.getTimestamp("start_time"));
reservation.setEndTime(rs.getTimestamp("end_time"));
reservation.setStatus(rs.getInt("status"));
list.add(reservation);
}
return list;
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, ps, rs);
}
return null;
}
public List<Reservation> getReservationsByVehicleId(int vehicleId) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = DBUtil.getConnection();
String sql = "SELECT * FROM reservation WHERE vehicle_id=?";
ps = conn.prepareStatement(sql);
ps.setInt(1, vehicleId);
rs = ps.executeQuery();
List<Reservation> list = new ArrayList<>();
while (rs.next()) {
Reservation reservation = new Reservation();
reservation.setId(rs.getInt("id"));
reservation.setUserId(rs.getInt("user_id"));
reservation.setVehicleId(rs.getInt("vehicle_id"));
reservation.setStartTime(rs.getTimestamp("start_time"));
reservation.setEndTime(rs.getTimestamp("end_time"));
reservation.setStatus(rs.getInt("status"));
list.add(reservation);
}
return list;
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, ps, rs);
}
return null;
}
public boolean isVehicleAvailable(int vehicleId, Date startTime, Date endTime) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = DBUtil.getConnection();
String sql = "SELECT COUNT(*) FROM reservation WHERE vehicle_id=? AND ((start_time<=? AND end_time>=?) OR (start_time<=? AND end_time>=?))";
ps = conn.prepareStatement(sql);
ps.setInt(1, vehicleId);
ps.setTimestamp(2, new Timestamp(startTime.getTime()));
ps.setTimestamp(3, new Timestamp(startTime.getTime()));
ps.setTimestamp(4, new Timestamp(endTime.getTime()));
ps.setTimestamp(5, new Timestamp(endTime.getTime()));
rs = ps.executeQuery();
if (rs.next()) {
int count = rs.getInt(1);
return count == 0;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, ps, rs);
}
return false;
}
}
```
以上是一个简单的车辆预约系统的代码框架,您可以根据实际情况进行修改和完善。
阅读全文