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为主键,返回的便是数据库中的记录数
你要记得给分啊
解决方案 »
- Java做的数据管理工具网页版,数据库管理工具
- No result defined for action com.score.student.action.StudentAction and result i
- 求一个Hibernate工具!
- myeclipse中源文件连接问题!
- 求tomcat重启bat脚本
- 高分问String中提取需要的数据怎么操作?
- struts2 如何实现页面一打开就触发action
- 如何自动生成jsp文件?
- structs入门:Invalid path was requested/hello
- JAVA 反射获取返回值为MAP类型中的key
- 有一个使用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