// update whether accept credit card public Restaurants updateAcceptsCreditCard(Restaurants restaurant, Boolean newaccept) throws SQLException { String updateStar = "UPDATE Restaurants SET AcceptsCreditCard=? WHERE RestaurantId=?;"; Connection connection = null; PreparedStatement updateStmt = null; try { connection = connectionManager.getConnection(); updateStmt = connection.prepareStatement(updateStar); updateStmt.setBoolean(1, newaccept); updateStmt.setString(2, restaurant.getRestaurantId()); updateStmt.executeUpdate(); restaurant.setAcceptsCreditCard(newaccept); return restaurant; } catch (SQLException e) { e.printStackTrace(); throw e; } finally { if (connection != null) { connection.close(); } if (updateStmt != null) { updateStmt.close(); } } }
// update open time public Restaurants updateOpenTime(Restaurants restaurant, Time newOpenTime) throws SQLException { String updateStar = "UPDATE Restaurants SET OpenTime=? WHERE RestaurantId=?;"; Connection connection = null; PreparedStatement updateStmt = null; try { connection = connectionManager.getConnection(); updateStmt = connection.prepareStatement(updateStar); updateStmt.setTime(1, newOpenTime); updateStmt.setString(2, restaurant.getRestaurantId()); updateStmt.executeUpdate(); restaurant.setOpen(newOpenTime); return restaurant; } catch (SQLException e) { e.printStackTrace(); throw e; } finally { if (connection != null) { connection.close(); } if (updateStmt != null) { updateStmt.close(); } } }
@Override public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // Map for storing messages. Map<String, String> messages = new HashMap<String, String>(); req.setAttribute("messages", messages); List<Reviews> reviewList = new ArrayList<Reviews>(); // Retrieve and validate name. String userName = req.getParameter("username"); String restaurantName = req.getParameter("restaurantname"); String restaurantId = req.getParameter("restaurantid"); List<Users> userList = new ArrayList<Users>(); List<Restaurants> restaurantList = new ArrayList<Restaurants>(); try { userList = usersDao.getUsersFromUserName(userName); restaurantList = restaurantsDao.getRestaurantByName(restaurantName); } catch (SQLException e) { e.printStackTrace(); throw new IOException(e); } if (userName != null && !userName.trim().isEmpty()) { for (Users user : userList) { int userId = user.getUserId(); try { reviewList.addAll(reviewsDao.getReviewsByUserId(userId)); } catch (SQLException e) { e.printStackTrace(); throw new IOException(e); } } req.setAttribute("reviews", reviewList); } else if (restaurantName != null && !restaurantName.trim().isEmpty()) { for (Restaurants rest : restaurantList) { String restId = rest.getRestaurantId(); System.out.println(" rId:" + restId); try { reviewList.addAll(reviewsDao.getReviewsByRestaurantId(restId)); } catch (SQLException e) { continue; } } req.setAttribute("reviews", reviewList); } else if (restaurantId != null && !restaurantId.trim().isEmpty()) { System.out.println(restaurantId); try { reviewList.addAll(reviewsDao.getReviewsByRestaurantId(restaurantId)); } catch (SQLException e) { e.printStackTrace(); throw new IOException(e); } req.setAttribute("reviews", reviewList); } req.getRequestDispatcher("/FindReviews.jsp").forward(req, resp); }
/** Delete the Restaurant instance. This runs a DELETE statement. */ public Restaurants delete(Restaurants restaurant) throws SQLException { String deleteRestaurant = "DELETE FROM Restaurants WHERE RestaurantId=?;"; Connection connection = null; PreparedStatement deleteStmt = null; try { connection = connectionManager.getConnection(); deleteStmt = connection.prepareStatement(deleteRestaurant); deleteStmt.setString(1, restaurant.getRestaurantId()); deleteStmt.executeUpdate(); // Return null so the caller can no longer operate on the Restaurant instance. return null; } catch (SQLException e) { e.printStackTrace(); throw e; } finally { if (connection != null) { connection.close(); } if (deleteStmt != null) { deleteStmt.close(); } } }
/** * Save the Persons instance by storing it in your MySQL instance. This runs a INSERT statement. */ public Restaurants create(Restaurants restaurant) throws SQLException { String insertRestaurant = "INSERT INTO Restaurants(RestaurantId,RestaurantName,AcceptsCreditCard,WIFI,PriceRange,OpenTime," + "CloseTime,NoiseLevel,Neighborhood,Star,Parking,Street,City,State,ZipCode) " + "VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);"; Connection connection = null; PreparedStatement insertStmt = null; try { connection = connectionManager.getConnection(); insertStmt = connection.prepareStatement(insertRestaurant); insertStmt.setString(1, restaurant.getRestaurantId()); insertStmt.setString(2, restaurant.getName()); insertStmt.setBoolean(3, restaurant.getAcceptsCreditCard()); insertStmt.setBoolean(4, restaurant.getWIFI()); insertStmt.setInt(5, restaurant.getPriceRange()); insertStmt.setTime(6, new Time(restaurant.getOpen().getTime())); insertStmt.setTime(7, new Time(restaurant.getClose().getTime())); insertStmt.setInt(8, restaurant.getNoiseLevel()); insertStmt.setString(9, restaurant.getNeighborhood()); insertStmt.setDouble(10, restaurant.getStar()); insertStmt.setInt(11, restaurant.getParking()); insertStmt.setString(12, restaurant.getStreet()); insertStmt.setString(13, restaurant.getCity()); insertStmt.setString(14, restaurant.getState()); insertStmt.setInt(15, restaurant.getZipCode()); insertStmt.executeUpdate(); return restaurant; } catch (SQLException e) { e.printStackTrace(); throw e; } finally { if (connection != null) { connection.close(); } if (insertStmt != null) { insertStmt.close(); } } }
/** Delete all rows from all tables */ public static void deleteAll() { Restaurants.delete(); }