Ejemplo n.º 1
0
 public static int creaUsuari(Usuari u) {
   if (existeixUsuari(u.getNomUsuari())) return -1;
   // Ja hem comprovat que no hi ha un usuari amb el mateix nom... per tant podem inserir be.
   try (PreparedStatement p = conn.prepareStatement(INSERT_USUARI)) {
     p.setString(1, u.getNomUsuari());
     p.setString(2, u.getContrasenya());
     p.setString(3, u.getNomReal());
     p.executeUpdate();
   } catch (SQLException e) {
     throw new RuntimeException(e);
   }
   return CapaPersistencia.retornaUltimaClauInserida();
 }
Ejemplo n.º 2
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;
 }
Ejemplo n.º 3
0
 public static boolean modificaUsuari(Usuari u) {
   if (!existeixUsuari(u.getUniqID())) return false;
   try (PreparedStatement s = conn.prepareStatement(UPDATE_USUARI)) {
     s.setString(1, u.getNomUsuari());
     s.setString(2, u.getContrasenya());
     s.setString(3, u.getNomReal());
     s.setInt(4, u.getUniqID());
     int modificats = s.executeUpdate();
     if (modificats != 1) {
       String problema;
       if (modificats == 0)
         throw new RuntimeException(
             "No s'ha modificat l'usuari, pero ha passat el check d'existencia.");
       else problema = String.format("S'han modificat %d usuaris!", modificats);
       throw new RuntimeException(problema);
     }
   } catch (SQLException e) {
     throw new RuntimeException(e);
   }
   return true;
 }
Ejemplo n.º 4
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;
 }