コード例 #1
0
  /**
   * Rate a restaurant and give a feedback Rate uses SQL INSERT INTO
   *
   * @param stars number of stars
   * @param feedback user's feedback
   * @param customer a customer
   * @return true if rate works fine, false otherwise
   * @throws SQLException
   */
  public boolean rate(int stars, String feedback, Customer customer) throws SQLException {
    boolean success = false;
    PreparedStatement insertStatement = null;
    int customerID = getCID(customer.getEmail());
    String query =
        "INSERT INTO Rating (cid, stars, feedback) VALUES (?,?,?) "
            + "ON DUPLICATE KEY UPDATE stars=?, feedback=?";

    try {
      connection.setAutoCommit(false);
      insertStatement = (PreparedStatement) connection.prepareStatement(query);
      insertStatement.setInt(1, customerID);
      insertStatement.setInt(2, stars);
      insertStatement.setString(3, feedback);
      insertStatement.setInt(4, stars);
      insertStatement.setString(5, feedback);
      insertStatement.execute();

      connection.commit();
      success = true;

    } catch (SQLException e) {
      e.printStackTrace();
      success = false;
    } finally {
      if (insertStatement != null) {
        insertStatement.close();
      }

      connection.setAutoCommit(true);
      return success;
    }
  }
コード例 #2
0
  /**
   * Reserve a table
   *
   * @param partySize number of people in a party
   * @param d reservation date
   * @param tID table id
   * @param c a customer
   * @return true if succeed, false otherwise
   */
  public boolean reserveTable(int partySize, Date d, int tID, Customer c) {
    PreparedStatement statement = null;
    int customerid = getCID(c.getEmail());
    String sql_reserve =
        "INSERT INTO Restaurant.Reservation (reservationDate,partySize,cID,tID) values(?, ?, ?,?)";

    try {
      connection.setAutoCommit(false);
      statement = (PreparedStatement) connection.prepareStatement(sql_reserve);
      statement.setDate(1, d);
      statement.setInt(2, partySize);
      statement.setInt(3, customerid);
      statement.setInt(4, tID);
      statement.executeUpdate();
      connection.commit();

      if (statement != null) {
        statement.close();
      }

      connection.setAutoCommit(true);

      return true;

    } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      System.out.println("Failed: " + e.getMessage());
      return false;
    }
  }