Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*******************
- ** DELETE USER **
- ******************/
- /* TA WERSJA MIAŁA DZIAŁAĆ MODYFIKUJAC ID PÓL AUTOINKREMENTOWALNYCH
- * NIESTETY NIE DZIALA PRZEZ TO ZE TE POLA SA UNIKALNE
- * WIEC ZEBY TA WERSJA MIALA SENS, TRZEBA BY WPROWADZIC
- * RECZNE PRZYPISYWANIE ID
- */
- public List<User> deletetUser(String l){
- List<User> users = new LinkedList<User>();
- users = selectUsers();
- boolean isUserFound = false;
- ///getting ID deleted users is required to decrease
- ///id next users, becouse query DELETE
- ///doesnt do that automatically
- int idDeletedUser = 0; //it should be safety becouse
- // in db fields are incrementing
- // from 1
- for(User u:users){
- System.out.println(u);
- if ( l.equals(u.getLogin()) ){
- idDeletedUser = u.getId();
- System.out.println("id kasowanego usera: " + idDeletedUser);
- isUserFound = true;
- }
- }
- if(isUserFound){
- ///user will be deleted from db
- try{
- String deleteUser = "DELETE FROM users_table WHERE login='"+l+"'";
- stat.execute(deleteUser);
- System.out.println("DELETE USER command executed!");
- }catch(SQLException e){
- e.printStackTrace();
- return null;
- }
- ///but it still exist at list
- users = selectUsers();
- ///so now users contains list without deleted user
- ///but id variables need to be decreased
- ///when user was last on the list its over..
- System.out.println("users.size()==" + users.size());
- if(idDeletedUser == users.size()){
- System.out.println("idDeletedUser == users.size()");
- return users;
- }
- //else its time to decrease ids next users
- else{
- System.out.println("! its time to decrease ids next users");
- int i=1; //List is indexed from 1
- for(User u:users){
- if( (idDeletedUser+1) == u.getId() ){
- System.out.println("WCHODZE DO IF");
- //users.get(i).setId(666); //nie mam ustawiac w liscie userowi tylko w bazie
- try{
- String updateTable = "UPDATE users_table SET id ="+i;
- stat.execute(updateTable);
- System.out.println("updateTable command executed!");
- }catch(SQLException e){
- System.out.println("updateTable trhow exception!");
- e.printStackTrace();
- return null;
- }
- idDeletedUser++;
- }
- i++;
- }
- }
- return users;
- }
- else{
- System.out.println("Nie znaleziono takiego usera");
- return users;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement