Reputation:
i have this method here i want find the most frequent value in a given column in an SQL table, the Sql code is working fine when i am trying it on MSSQL but when i am trying to return the values it is giving me Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to BLL.Perdoruesi
public Perdoruesi repeatedUsername() throws PerdoruesiException{
String sql =" select p.Username ,count(*) as nor from Perdoruesi p\n" +
" group by Username\n" +
" having count(*) =(select max(nor) from \n" +
" (select Username,count(*) as nor from Perdoruesi group by Username) Perdoruesi)";
Query query = em.createNativeQuery(sql);
try{
return (Perdoruesi)query.getSingleResult();
}catch(NoResultException e){
throw new PerdoruesiException("..");
}
}
Upvotes: 0
Views: 318
Reputation:
Solved it
public String []repeatedUsername() throws PerdoruesiException{
String sql =" select p.Username ,count(*) as nor from Perdoruesi p\n" +
" group by Username\n" +
" having count(*) =(select max(nor) from \n" +
" (select Username,count(*) as nor from Perdoruesi group by Username) Perdoruesi)";
String p = null;
int numri = 0;
Query query = em.createNativeQuery(sql);
List<Object[]> list = query.getResultList();
for(Object [] obj : list){
p = (String)obj[0];
numri = (int) obj[1];
}
return new String[]{p,String.valueOf(numri)};
}
Upvotes: 0