一个struts+hibernate的项目,其中有一个地方每次执行就java.lang.ClassCastException: java.lang.Long然后根据提示错误的地方去找:
public static int findTCount() {
Transaction tx = null;
int intCount= 0;
try {
session = hib.openSession();
tx = session.beginTransaction();
intCount=((Integer)session.createQuery(
"select count(*)from Tongxun").uniqueResult()).intValue();
tx.commit();
hib.closeSession(session);
} catch (Exception e) {
e.printStackTrace();
tx.rollback();
}
return intCount;
}其中intCount=((Integer)session.createQuery("select count(*)from Tongxun").uniqueResult()).intValue();是这句话出的问题,请路过的高人指教,小弟不胜感激。

解决方案 »

  1.   

    你调试看下,是不是session.createQuery("select count(*)from Tongxun").uniqueResult()这个结果是Long,而你强转成Integer,出现了ClassCastException
      

  2.   

    类型转换异常,你看看count(*)from 中间是不是没加空格啊
      

  3.   

    修改为:
    intCount=Integer.parseInt(session.createQuery("select count(*)from Tongxun").uniqueResult());
    试试看.
      

  4.   

    应该是你SQL语句查出来的是Long型的 要转换成int型的话
    Long a = 10L;
    int b = Integer.parseInt(a);
      

  5.   

    楼主直接用BigDecimal就不会出现这些西西了。