Advertisement
Guest User

Untitled

a guest
Sep 4th, 2015
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.99 KB | None | 0 0
  1.  /*******************
  2.      ** DELETE USER **
  3.      ******************/
  4.     /* TA WERSJA MIAŁA DZIAŁAĆ MODYFIKUJAC ID PÓL AUTOINKREMENTOWALNYCH
  5.      * NIESTETY NIE DZIALA PRZEZ TO ZE TE POLA SA UNIKALNE
  6.      * WIEC ZEBY TA WERSJA MIALA SENS, TRZEBA BY WPROWADZIC
  7.      * RECZNE PRZYPISYWANIE ID     
  8.     */
  9.     public List<User> deletetUser(String l){
  10.        
  11.         List<User> users = new LinkedList<User>();
  12.                
  13.         users = selectUsers();
  14.         boolean isUserFound = false;
  15.         ///getting ID deleted users is required to decrease
  16.         ///id next users, becouse query DELETE
  17.         ///doesnt do that automatically    
  18.         int idDeletedUser = 0; //it should be safety becouse
  19.                                // in db fields are incrementing
  20.                                // from 1
  21.        
  22.         for(User u:users){
  23.             System.out.println(u);
  24.             if ( l.equals(u.getLogin()) ){             
  25.                 idDeletedUser = u.getId();
  26.                 System.out.println("id kasowanego usera: " + idDeletedUser);
  27.                 isUserFound = true;
  28.             }  
  29.         }
  30.        
  31.         if(isUserFound){
  32.             ///user will be deleted from db
  33.             try{
  34.                
  35.                 String deleteUser = "DELETE FROM users_table WHERE login='"+l+"'";
  36.                 stat.execute(deleteUser);
  37.                 System.out.println("DELETE USER command executed!");       
  38.             }catch(SQLException e){
  39.                 e.printStackTrace();
  40.                 return null;
  41.             }
  42.             ///but it still exist at list      
  43.             users = selectUsers();
  44.            
  45.             ///so now users contains list without deleted user
  46.             ///but id variables need to be decreased
  47.            
  48.             ///when user was last on the list its over..           
  49.             System.out.println("users.size()==" + users.size());
  50.             if(idDeletedUser == users.size()){
  51.                  System.out.println("idDeletedUser == users.size()");
  52.                 return users;  
  53.             }
  54.             //else its time to decrease ids next users
  55.             else{
  56.                 System.out.println("! its time to decrease ids next users");                
  57.                 int i=1; //List is indexed from 1
  58.                 for(User u:users){
  59.                     if( (idDeletedUser+1) == u.getId() ){
  60.                         System.out.println("WCHODZE DO IF");
  61.                         //users.get(i).setId(666); //nie mam ustawiac w liscie userowi tylko w bazie
  62.                         try{
  63.                             String updateTable = "UPDATE users_table SET id ="+i;
  64.                             stat.execute(updateTable);
  65.                             System.out.println("updateTable command executed!");       
  66.                         }catch(SQLException e){
  67.                             System.out.println("updateTable trhow exception!");
  68.                             e.printStackTrace();
  69.                             return null;
  70.                         }
  71.                         idDeletedUser++;
  72.                     }
  73.                     i++;
  74.                 }
  75.             }
  76.             return users;      
  77.         }
  78.         else{
  79.             System.out.println("Nie znaleziono takiego usera");
  80.             return users;
  81.         }  
  82.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement