SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session s = sf.openSession();
Transaction tx = s.beginTransaction(); try {

Integer count = (Integer)s.createQuery("select count(id) as ids from Orders o").uniqueResult();
out.println("数量 = " + count.toString());
} catch (Exception e) {
tx.rollback();
s.close();
out.println("出错" + e);
}
其中id为主键,返回的便是数据库中的记录数
你要记得给分啊

解决方案 »

  1.   

    public static int getErrandArticleNUM(long errandid) {
        int num = 0;
        Session session = null;
        try {
          session = sessionFactory.openSession();
          String hql = "select count(id) FROM errandarticle";
          System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<SQL:" + hql);
          Integer integer = (Integer) session.createQuery(hql).uniqueResult();
          num = integer.intValue();
        } catch (HibernateException ex) {
          ex.printStackTrace();
        }finally{
          try {
            session.close();
          } catch (HibernateException ex1) {
          }
        }
        return num;
      }
    <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<SQL:select count(id) FROM errandarticle
    Hibernate: select  from
    14:24:47,921  WARN JDBCExceptionReporter:57 - SQL Error: 156, SQLState: 01000
    14:24:47,937 ERROR JDBCExceptionReporter:58 - [CDFG]在关键字 'from' 附近有语法错误。
    14:24:47,937  WARN JDBCExceptionReporter:57 - SQL Error: 156, SQLState: 01000
    14:24:47,937 ERROR JDBCExceptionReporter:58 - [CDFG]在关键字 'from' 附近有语法错误。
    net.sf.hibernate.exception.GenericJDBCException: Could not execute query
    at net.sf.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:81)
    at net.sf.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:70)
    at net.sf.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:30)
    at net.sf.hibernate.impl.SessionImpl.convert(SessionImpl.java:4110)
    at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1556)
    at net.sf.hibernate.impl.QueryImpl.list(QueryImpl.java:39)
    at net.sf.hibernate.impl.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:550)
    at cn.com.anbis.forum.ErrandTools.getErrandArticleNUM(ErrandTools.java:431)
    at cn.com.anbis.forum.Test.main(Test.java:45)
    Caused by: java.sql.SQLException: [CDFG]在关键字 'from' 附近有语法错误。
    at com.inet.tds.c.a(Unknown Source)
    at com.inet.tds.b.do(Unknown Source)
    at com.inet.tds.b.executeQuery(Unknown Source)
    at net.sf.hibernate.impl.BatcherImpl.getResultSet(BatcherImpl.java:88)
    at net.sf.hibernate.loader.Loader.getResultSet(Loader.java:875)
    at net.sf.hibernate.loader.Loader.doQuery(Loader.java:269)
    at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
    at net.sf.hibernate.loader.Loader.doList(Loader.java:1033)
    at net.sf.hibernate.loader.Loader.list(Loader.java:1024)
    at net.sf.hibernate.hql.QueryTranslator.list(QueryTranslator.java:854)
    at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1553)
    ... 4 more
    14:24:47,968  WARN JDBCExceptionReporter:34 - SQL Warning: 5701, SQLState: 01000
    14:24:47,968  WARN JDBCExceptionReporter:35 - [CDFG]已将数据库上下文改为 'songzy'。
    14:24:47,968  WARN JDBCExceptionReporter:34 - SQL Warning: 5703, SQLState: 01000
    14:24:47,968  WARN JDBCExceptionReporter:35 - [CDFG]已将语言设置改为 简体中文。
    0
      

  2.   

    s.createSQLQuery("select count(id) as ids from user")