带别名的MySQL select语句失败,找不到列

时间:2013-09-11 00:20:54

标签: mysql column-alias

开发环境参考:

Fedora FC18
Tomcat Server 7.0.39 (localhost config)
Eclipse Juno Release 2
Mysql-connecor-java Ver. 5.1.26 (jar)
Mysql-server Ver. 5.5.32 (jar)

以下选择语句失败,其中"列'深度'找不到":

"select node.subEntityID, node.lft, node.rgt, (count(parent.subEntityID) - 1) as depth
 from ENTITY as node, ENTITY as parent
 where node.lft between parent.lft and parent.rgt
 group by node.subEntityID order by node.lft";

此select语句成功使用命令级mysql作为

use dbName;
select node.subEntityID, node.lft, node.rgt, (count(parent.subEntityID) - 1) as depth
  from ENTITY as node, ENTITY as parent
  where node.lft between parent.lft and parent.rgt
  group by node.subEntityID order by node.lft;

在MySQL Workbench ver中使用SQL查询时,select语句也会成功。 5.2.4.7。

相关的Java代码片段如下:

rs = stmt.executeQuery(typeEntityList);
// The depth alias does not print out in this for loop
for (int i=1; i<rs.getMetaData().getColumnCount()+1; i++) {
    System.out.println(rs.getMetaData().getColumnName(i));
    System.out.println(rs.getMetaData().getColumnLabel(i));
}
while (rs.next()) {
    // "depth" fails at the following statement
    System.out.println("depth: " + rs.getInt("depth"));
    String s = rs.getString("lft") + "  "
        + rs.getString("subEntityID") + "  "
        + rs.getString("rgt");
    System.out.println(s);
    entityList.add(s);
}

此外,我在Google搜索中发现MySQL的别名行为在最近的版本中发生了变化。建议的修复方法是将"?useOldAliasMetadataBehavior=true"附加到MySQL连接字符串以恢复到原始行为,但这不能解决问题。

这是堆栈跟踪:

  

java.sql.SQLException:列&#39;深度&#39;未找到。在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078)在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:975 )在com.mysql.jdbc.ResultSetImpl.findColumn(ResultSetImpl.java:1167)的com.mysql.jdbc.SQLError.createSQLException(SQLError.java:920)com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java: 5733)在   EntityAssessmentEntityActions.EntityAssessmentEntityListAction.detailedEntityList(EntityAssessmentEntityListAction.java:184)在EntityAssessmentEntityActions.EntityAssessmentEntityListAction.execute(EntityAssessmentEntityListAction.java:69)在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java: 57)在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)在java.lang.reflect.Method.invoke(Method.java:606)在com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:450 )在com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:289)在com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:252)在org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept( Debug.Interceptor.java:256)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvoc) ation.java:246)在com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)在com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)在com.opensymphony.xwork2位于org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)的com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)中的.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)。 )在com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)在com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)在com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept( ConversionErrorInterceptor.java:138)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)at com.opensymphony.xwork2.intercep tor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:249)com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)at com .opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:249)在com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)在com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java :246)在com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:191)在com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)在org.apache.struts2.interceptor.MultiselectInterceptor。在org.apache.struts2.intercept上的com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)拦截(MultiselectInterceptor.java:73) or.CheckboxInterceptor.intercept(CheckboxInterceptor.java:91)在com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)在org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:252)在COM .opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) )com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept( ChainingInterceptor.java:145)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)at com.opensymphony.xwork2.interceptor.PrepareInterceptor.d oIntercept(PrepareInterceptor.java:171)在com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)在com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)在com.opensymphony.xwork2 .interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176)在com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)在org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)在com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)在com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:193)在com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java: 246)com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.j) ava:246)org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:546)org.apache.struts2.dispatcher .ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain) .java:243)org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)位于org.apache.catalina的org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)。 core.tandardContextValve.invoke(StandardContextValve.java:123)org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)在org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:9 9)org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:947)org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)org.apache.catalina.connector.CoyoteAdapter 。服务(CoyoteAdapter.java:408)在org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1009)在org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:589)的组织。 apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:312)java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)at java.util.concurrent.ThreadPoolExecutor $ Worker.run( ThreadPoolExecutor.java:615)在java.lang.Thread.run(Thread.java:724)

2 个答案:

答案 0 :(得分:0)

StackOverflow成员,我发现了这个问题,但是现在我因为把头发拉出来而秃顶。

在Eclipse Juno上,选择了Java源代码的“Organnize Imports”。“所以当我有一个未定义的变量时,我使用这个方便的选项来清理导入.Eclipse Juno导入了mysql连接器,”java .sql.connection“而不是”com.mysql.jdbc.connection“连接器。我更改了java源代码的导入,现在我很高兴继续使用这个servlet。但是在我出去找到一些之后就没有了。 Propecia或Rogaine或?... ... Roy W。

答案 1 :(得分:0)

我在项目中遇到了类似的问题(我正在使用Eclipse Luna)。

我克服它的方法是将 MysqlDataSource 设置为 setUseOldAliasMetadataBehavior(true);