-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseManager.java
More file actions
124 lines (115 loc) · 4.79 KB
/
DatabaseManager.java
File metadata and controls
124 lines (115 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import java.sql.*;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
public class DatabaseManager {
private static final String DB_URL = "jdbc:mysql://localhost:3306/car_rental_system";
private static final String DB_USER = System.getenv("DB_USER") != null ? System.getenv("DB_USER") : "root";
private static final String DB_PASSWORD = System.getenv("DB_PASSWORD") != null ? System.getenv("DB_PASSWORD") : "root"; // Change this to your MySQL root password
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
}
public static List<Car> getAllCars() {
List<Car> cars = new ArrayList<>();
String sql = "SELECT * FROM cars";
try (Connection conn = getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
while (rs.next()) {
cars.add(new Car(
rs.getString("car_id"),
rs.getString("brand"),
rs.getString("model"),
rs.getDouble("base_price_per_day"),
rs.getBoolean("is_available")
));
}
} catch (SQLException e) {
e.printStackTrace();
}
return cars;
}
public static void addCar(Car car) {
String sql = "INSERT INTO cars (car_id, brand, model, base_price_per_day, is_available) VALUES (?, ?, ?, ?, ?)";
try (Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, car.getCarId());
pstmt.setString(2, car.getBrand());
pstmt.setString(3, car.getModel());
pstmt.setDouble(4, car.getBasePricePerDay());
pstmt.setBoolean(5, car.isAvailable());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void updateCarAvailability(String carId, boolean isAvailable) {
String sql = "UPDATE cars SET is_available = ? WHERE car_id = ?";
try (Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setBoolean(1, isAvailable);
pstmt.setString(2, carId);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static int addCustomer(String name) {
String sql = "INSERT INTO customers (name) VALUES (?)";
int customerId = 0;
try (Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
pstmt.setString(1, name);
pstmt.executeUpdate();
ResultSet rs = pstmt.getGeneratedKeys();
if (rs.next()) {
customerId = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}
return customerId;
}
public static void addRental(String carId, int customerId, int rentalDays) {
String sql = "INSERT INTO rentals (car_id, customer_id, rental_days, rental_date) VALUES (?, ?, ?, ?)";
try (Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, carId);
pstmt.setInt(2, customerId);
pstmt.setInt(3, rentalDays);
pstmt.setDate(4, Date.valueOf(LocalDate.now()));
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static Rental getRentalByCarId(String carId) {
String sql = "SELECT * FROM rentals WHERE car_id = ? ORDER BY rental_date DESC LIMIT 1";
try (Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, carId);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
return new Rental(
rs.getInt("rental_id"),
rs.getString("car_id"),
rs.getInt("customer_id"),
rs.getInt("rental_days")
);
}
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public static void removeRental(int rentalId) {
String sql = "DELETE FROM rentals WHERE rental_id = ?";
try (Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, rentalId);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
}