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为主键,返回的便是数据库中的记录数
你要记得给分啊
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为主键,返回的便是数据库中的记录数
你要记得给分啊
解决方案 »
- 为什么不能给自己打分
- 收入
- 怎么在hibernate 中执行一个update的sql语句?
- 600分!JMS选型,ActiveMQ还是JORAM?
- [struts]Action为什么找到了但是没有执行?
- 紧急求助..服务端向客户端发送信息不成功.郁闷!
- JCreater pro 找不到javax.ejb包,怎么办?谢谢!!
- jbuilder9 中自带的例子,Esite (for weblogic) 运行时候出错:javax.naming.NameNotFoundException: Unable to resolve 'Cart' Resolved ;
- jboss-3.0.8_tomcat-4.1.24下documentation-example例子的配置运行问题??
- 有关 J2EE 的两个小问题
- 有一个使用Hibernate连接SqlServer的问题
- hibernate 里是怎么判断用户登陆失效的
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