
How to get repeate occurence values from database table in java by using arraylist

import java.sql.*;
import java.util.*;
class GetRepeatedValuesFromDatabase
{
public static <T> List getDuplicate(Collection<T> list) {
final List<T> l = new ArrayList<T>();
Set<T> set = new HashSet<T>() {
public boolean add(T e) {
if (contains(e)) {
l.add(e);
}
return super.add(e);
}
};
for (T t : list){
set.add(t);
}
return l;
}
public static <T> boolean hasDuplicate(Collection<T> list) {
if(getDuplicate(list).isEmpty())
return false;
return true;
}
public static void main(String[] args)
{
ArrayList<String> list=new ArrayList<String>();
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from data");
String name;
while(rs.next()){
list.add(rs.getString("name"));
}
if(hasDuplicate(list)){
List l=getDuplicate(list);
for(int i=0;i<l.size();i++){
System.out.println(l.get(i).toString());
}
}
}
catch(Exception e){}
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.