Beispiel #1
0
 public static Usuari donaUsuari(int id) {
   Usuari u = null;
   try (PreparedStatement s = conn.prepareStatement(SELECT_USUARI_ID)) {
     s.setInt(1, id);
     ResultSet resSet = s.executeQuery();
     if (resSet.next()) {
       u = new Usuari();
       u.setUniqID(resSet.getInt("id"));
       u.setNomUsuari(resSet.getString("nomUsuari"));
       u.setContrasenya(resSet.getString("contrasenya"));
       u.setNomReal(resSet.getString("nomReal"));
     }
     if (resSet.next()) {
       throw new RuntimeException("Hi ha mes d'un usuari amb la mateixa ID!");
     }
   } catch (SQLException e) {
     throw new RuntimeException(e);
   }
   return u;
 }
Beispiel #2
0
 public static Usuari donaUsuari(String nomUsuari) {
   Usuari u = null;
   try (PreparedStatement s = conn.prepareStatement(SELECT_USUARI_NOMUSUARI)) {
     s.setString(1, nomUsuari);
     ResultSet resSet = s.executeQuery();
     if (resSet.next()) {
       u = new Usuari();
       u.setUniqID(resSet.getInt("id"));
       u.setNomUsuari(resSet.getString("nomUsuari"));
       u.setContrasenya(resSet.getString("contrasenya"));
       u.setNomReal(resSet.getString("nomReal"));
     }
     if (resSet.next()) // Hi ha mes d'un usuari que concorda amb el nom que li ha donat! Error!!!
     {
       throw new RuntimeException("Hi ha mes d'un usuari amb el mateix nom!");
       // Aixo realment no te que ser RuntimeException, sino que hauria de ser normal i handlejada
       // mes amunt...
       // pero *tampoc passara mai* (la taula te UNIQUE constraint), aixi que f**k it.
     }
   } catch (SQLException e) {
     throw new RuntimeException(e);
   }
   return u;
 }