Results 1 to 4 of 4
  1. #1
    vector_ever is offline Senior Member
    Join Date
    Jul 2013
    Posts
    187
    Rep Power
    2

    Default delete the duplicated entries

    Hello,

    I have Table with some duplicated entries
    XML Code:
    StateId	   State_Name
    1	         BW
    2	         BY
    3	         BY
    4	         BE
    5	         BB
    6	         BR
    7	         HH
    8	         HH
    9	         HH
    10	         MV
    11	         MV
    12	         NW
    13	         RP
    14	         NW
    15	         BB
    Where StateId is Auto_increment, and as you notice there are some duplicated value in State_Name column, i want to delete them to have table like this
    XML Code:
    StateId	   State_Name
    1	         BW
    2	         BY
    3	         BE
    4	         BB
    5	         BR
    6	         HH
    7	         MV
    8	         NW
    9	         RP
    I want to delete those duplicated entries during insert or after it
    Java Code:
    query = "INSERT IGNORE into eumonis.federalState values(?,?)";
    ps = con.prepareStatement(query);
                                   
    Iterator<FederalState> it = dataHolder.iterator();
                   while (it.hasNext()){
                       FederalState obj = it.next();
                       
                   ps.setInt(1, obj.getStateId());
                   ps.setString(2, obj.getState().toString());
     
                     ps.executeUpdate();
                    }
    I tried to use ALTER IGNORE, like this:
    Java Code:
    String alter = "ALTER IGNORE TABLE eumonis.federalState ADD UNIQUE INDEX (State_Name, stateId)";
    			 ps = con.prepareStatement(alter);
    			ps.executeUpdate()
    ;

    but i don't see any thing happend, maybe i use it not correctly, so how to change my code to get the second table

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,784
    Rep Power
    5

    Default Re: delete the duplicated entries

    Why not use an implementation of the Set interface which cannot contain duplicates?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    vector_ever is offline Senior Member
    Join Date
    Jul 2013
    Posts
    187
    Rep Power
    2

    Default Re: delete the duplicated entries

    I use Set, but i content of Set change every time (according to reading from another resource every time and store it in the Set) so one time i have guarantee to have no duplicated values, but when run the code another time it will have another Set with could have same value with previous Set

    The only solution is to delete them from the table

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: delete the duplicated entries

    Set a unique index on that field.

Similar Threads

  1. HashSet accept duplicated Value
    By vector_ever in forum New To Java
    Replies: 24
    Last Post: 08-05-2013, 01:36 PM
  2. Need Help!Duplicated IO Class
    By oszc in forum New To Java
    Replies: 1
    Last Post: 11-05-2011, 07:40 AM
  3. data get duplicated on a Vector
    By khlitoshi in forum New To Java
    Replies: 15
    Last Post: 04-05-2011, 08:44 AM
  4. Modify and delete certain xml entries
    By scheilaad in forum XML
    Replies: 2
    Last Post: 02-28-2011, 11:18 PM
  5. Not able to delete MSSql database table entries
    By wickedrahul9 in forum Advanced Java
    Replies: 3
    Last Post: 10-17-2008, 12:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •