我在这里有此方法,我想在SQL表的给定列中找到最频繁的值,
当我在MSSQL上尝试使用Sql代码时,它工作正常,但是当我尝试返回值时,则在线程“ main”中给我Exception java.lang.ClassCastException:[Ljava.lang.Object;不能强制转换为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("..");
        }
    }

最佳答案

解决了

 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)};
    }

10-01 09:19