-1

I am trying to find duplicate rows from my resultset. I have a large data in resultset, which is the best way to find duplicates from resultset? I've tried with arraylist too.

List inner=new ArrayList<String>();
Connection con;
Statement stmt;
ResultSet rs;
ResultSetMetaData rsmd;
int columnNumber;

try{
    Class.forName("com.mysql.jdbc.Driver");
    con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root","");
    stmt=con.createStatement();
    rs=stmt.executeQuery("select * from mydata_table where srno<1000");
    rsmd=rs.getMetaData();
    columnNumber=rsmd.getColumnCount();
    while(rs.next()){

        for(int i=1;i<columnNumber;i++){
            inner.add(rs.getString(i));
        }

   }           
   System.out.println("\n" + inner);

   rs.close();
   con.close();
}catch(Exception e){
    System.out.println(e);
} 

I want to compare one row with remaining rows and find duplicate rows from the list.

Enamul Hassan
  • 5,266
  • 23
  • 39
  • 56
iks_in
  • 133
  • 2
  • 17

1 Answers1

0

If you sort the records in the query (probably ORDER BY ...) you then only need to compare each record with it's successor.

OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
  • Thank you for your help.But there are more than 40 columns in it, order by with which column? – iks_in Aug 17 '16 at 13:01
  • @iks_in - All of them if you like. Or as many of them that will make the duplicate check easy. If, for example, columns 1,2 and 3 are generally unique in most records, sort by them and then only search forward while all of them are the same. – OldCurmudgeon Aug 17 '16 at 13:04