Example #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();
 }
Example #2
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;
 }