Exemplo n.º 1
0
  public Utilisateur connexion(String login, String pass) throws ConnexionException {
    Utilisateur user = new Utilisateur();
    user.setMail("");

    try {
      Context ctx = new InitialContext();
      DataSource source = (DataSource) ctx.lookup("jdbc/MusicStore");
      connexion = source.getConnection();

      String requeteSQL =
          "SELECT motdepasse, prenom, IDUTILISATEUR FROM utilisateur WHERE mail = LCASE(?)";
      PreparedStatement prepStat = connexion.prepareStatement(requeteSQL);
      prepStat.setString(1, login);
      ResultSet donnees = prepStat.executeQuery();

      while (donnees.next()) {
        String passTest = donnees.getString(1);
        if (passTest.equals(pass) != true) {
          throw new ConnexionException("wrongPass");
        } else {
          user.setMail(login);
          user.setPrenom(donnees.getString(2));
          user.setIdUtilisateur(donnees.getInt(3));
        }
      }

    } catch (SQLException e) {
      throw new ConnexionException("sqlConnexionError");
    } catch (NamingException e) {
      throw new ConnexionException("errorNaming");
    } finally {
      try {
        connexion.close();
      } catch (SQLException e) {
        throw new ConnexionException("sqlConnexionError");
      }
    }
    return user;
  }