diff --git a/src/main/java/datamodel/Database.java b/src/main/java/datamodel/Database.java index bc09d82..4524220 100755 --- a/src/main/java/datamodel/Database.java +++ b/src/main/java/datamodel/Database.java @@ -197,14 +197,14 @@ public Show getShowData(String movie, String date) { } public Reservation addReservation(String username, String movie, String date) { - if(!isConnected()) { + if (!isConnected()) { System.err.println("addReservation: no connection to database"); return null; } System.out.println("Adding reservation for " + username + " for " + movie + " on " + date); - try(Statement stmt = conn.createStatement()) { + try (Statement stmt = conn.createStatement()) { conn.setAutoCommit(false); Show s = getShowData(movie, date); @@ -213,27 +213,46 @@ public Show getShowData(String movie, String date) { return null; } - ResultSet rs = stmt.executeQuery("SELECT * FROM Users WHERE username = '" + username + "'"); - if (rs.next()) { - int user_id = rs.getInt("id"); - rs = stmt.executeQuery("SELECT * FROM Showings JOIN Movies ON Showings.movie_id = Movies.movie_id WHERE name = '" + movie + "' AND show_date = '" + date + "'"); - if (rs.next()) { - System.out.println("Adding reservation"); - int showing_id = rs.getInt("showing_id"); - String theater = rs.getString("theater_id"); - stmt.executeUpdate("INSERT INTO Reservations (user_id, showing_id) VALUES (" + user_id + ", " + showing_id + ")"); - System.out.println("Reservation added"); - conn.commit(); - conn.setAutoCommit(true); - int bookingId = stmt.getGeneratedKeys().getInt(1); - return new Reservation(bookingId, movie, date, theater); + try (PreparedStatement userQuery = conn.prepareStatement("SELECT * FROM Users WHERE username = ?"); + PreparedStatement showingQuery = conn.prepareStatement("SELECT * FROM Showings JOIN Movies ON Showings.movie_id = Movies.movie_id WHERE name = ? AND show_date = ?"); + PreparedStatement reservationInsert = conn.prepareStatement("INSERT INTO Reservations (user_id, showing_id) VALUES (?, ?)", Statement.RETURN_GENERATED_KEYS)) { + + userQuery.setString(1, username); + try (ResultSet rs = userQuery.executeQuery()) { + if (rs.next()) { + int user_id = rs.getInt("id"); + + showingQuery.setString(1, movie); + showingQuery.setString(2, date); + try (ResultSet showingRs = showingQuery.executeQuery()) { + if (showingRs.next()) { + // Getting the data from the showing query + int showing_id = showingRs.getInt("showing_id"); + String theater = showingRs.getString("theater_id"); + + // Preparing the reservation insert with the data previously retrieved + reservationInsert.setInt(1, user_id); + reservationInsert.setInt(2, showing_id); + reservationInsert.executeUpdate(); + + // Committing the transaction and getting the generated keys + conn.commit(); + conn.setAutoCommit(true); + + // Getting the generated keys, if any + try (ResultSet generatedKeys = reservationInsert.getGeneratedKeys()) { + if (generatedKeys.next()) { + int bookingId = generatedKeys.getInt(1); + return new Reservation(bookingId, movie, date, theater); + } + } + } + } + } else { + System.err.println(String.format("addReservation: user %s not found", username)); + } } - rs.close(); } - else { - System.err.println(String.format("addReservation: user %s not found", username)); - } - conn.commit(); } catch (SQLException e) { try { conn.rollback(); @@ -245,6 +264,7 @@ public Show getShowData(String movie, String date) { return null; } + public ArrayList getReservations(String username) { ArrayList reservations = new ArrayList();