java代码中执行包含count()函数的执行问题

J2EE 码拜 10年前 (2015-04-12) 1046次浏览 0个评论

sql=”select id,classifiedName,personNumber,hospitalDepartment,name,COUNT(deptCode) from ” +
“((select thc.id ,thc.classifiedName,thc.personNumber,thc.hospitalDepartment,td.name ” +
“FROM t_hpdepartmentclassified thc,t_department td WHERE (thc.classifiedName LIKE “”%医生%””” +
” OR thc.classifiedName like “”%护士%””) and td.id=thc.hospitalDepartment) a” +
” LEFT JOIN( select thr.deptCode,trd.localclassField from  t_hr thr,t_referencedept trd ” +
“where thr.qMonth=””08″” and thr.qYear=””2014″” and trd.hrDeptId=thr.deptCode ) b ” +
“on(a.id = b.localclassField) )GROUP BY localclassField,classifiedName,personNumber,” +
“hospitalDepartment ORDER BY hospitalDepartment ASC”;
这是java代码中执行的sql语句,在mysql中执行没有任何问题,能查询出结果,但是到java代码中执行就报错:(搞了两天了都没排查出来错误,希望各位大神帮帮忙)

myeclipse上包的错误:

ERROR 08-09 00:48:14,124 – line 1:22: unexpected token: (
ERROR 08-09 00:48:14,125 – line 1:106: unexpected token: FROM

页面报的错误信息:

type Exception report

message unexpected token: ( near line 1, column 22 [select count(*) from ((select thc.id ,thc.classifiedName,thc.personNumber,thc.hospitalDepartment,td.name FROM t_hpdepartmentclassified thc,t_department td WHERE (thc.classifiedName LIKE “”%医生%”” OR thc.classifiedName like “”%护士%””) and td.id=thc.hospitalDepartment) a LEFT JOIN( select thr.deptCode,trd.localclassField from t_hr thr,t_referencedept trd where thr.qMonth=””08″” and thr.qYear=””2014″” and trd.hrDeptId=thr.deptCode ) b on(a.id = b.localclassField) )GROUP BY localclassField,classifiedName,personNumber,hospitalDepartment ORDER BY hospitalDepartment ASC]

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.hibernate.hql.ast.QuerySyntaxException: unexpected token: ( near line 1, column 22 [select count(*) from ((select thc.id ,thc.classifiedName,thc.personNumber,thc.hospitalDepartment,td.name FROM t_hpdepartmentclassified thc,t_department td WHERE (thc.classifiedName LIKE “”%医生%”” OR thc.classifiedName like “”%护士%””) and td.id=thc.hospitalDepartment) a LEFT JOIN( select thr.deptCode,trd.localclassField from  t_hr thr,t_referencedept trd where thr.qMonth=””08″” and thr.qYear=””2014″” and trd.hrDeptId=thr.deptCode ) b on(a.id = b.localclassField) )GROUP BY localclassField,classifiedName,personNumber,hospitalDepartment ORDER BY hospitalDepartment ASC]
org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:31)
org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:24)
org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:59)
org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:258)
org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:157)
org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)
com.boray.dao.IdaoImpl.getPageCount(IdaoImpl.java:128)
com.boray.dao.IdaoImpl.getPager2(IdaoImpl.java:99)
com.boray.dao.IdaoImpl.getPager2(IdaoImpl.java:113)
com.boray.base.AbstractBaseDao.getPager2(AbstractBaseDao.java:58)
com.boray.action.DoctorNursingNumManagerAction.findDnPage(DoctorNursingNumManagerAction.java:98)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:601)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:453)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:292)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:255)
org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)
org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:236)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:236)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:90)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:192)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:511)
org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)

note The full stack trace of the root cause is available in the Apache Tomcat/6.0.39 logs.

java代码中执行包含count()函数的执行问题
20分
看提示好想是左右括号不匹配。
你这样弄好了。 你先写一个最简单的sql,然后一句句加上去,看加到哪句就报错了。
java代码中执行包含count()函数的执行问题
10分
debug中将执行前的sql取出,然后放到里面执行一下看看,估计是转义错误
java代码中执行包含count()函数的执行问题
我经过代码修改后,还是不行;页面中提示的错误是:
 t_hr is not mapped [select count(*) from t_hr where qMonth=””08″” and qYear=””2014″” GROUP BY deptCode]
已经精简到这样了。用debug生成的是这样的:
select deptCode,deptCode from  t_hr  where  qMonth=””08″” and qYear=””2014″” GROUP BY deptCode
在数据库中执行没问题啊
在myeclipse中执行的sql包含count()函数应该可以执行?(之前这样写的sql都没这种问题,求解)
java代码中执行包含count()函数的执行问题
10分
感觉是 GROUP BY用错了。
你是要计算分组之后的count。你那个sql感觉不对
java代码中执行包含count()函数的执行问题
谢了各位,问题找到了,是我调用的底层dao有问题,不能传复杂的sql,跟大家分享一下

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明java代码中执行包含count()函数的执行问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!