java.sql.SQLException:语法错误

Posted

技术标签:

【中文标题】java.sql.SQLException:语法错误【英文标题】:java.sql.SQLException: Syntax Error 【发布时间】:2015-01-14 14:28:35 【问题描述】:

当我查询 CSV 文件(使用 relique CsvDriver)时,我在下面收到此错误消息。

。在第 1 行第 105 列遇到““GROUP”。 期待其中之一: “和” ... “或者” ... “命令” ... “限制” ...

这是我的代码

Class.forName("org.relique.jdbc.csv.CsvDriver");

Connection conn = DriverManager.getConnection("jdbc:relique:csv:" + mgrDir);

Statement stmt = conn.createStatement();

String query = "SELECT TRASH_TYPE AS trash_type, COUNT(TRASH_TYPE) AS num FROM " + src_file + " WHERE TRASH_DATE LIKE '" + query_date + "' GROUP BY TRASH_TYPE";

ResultSet results = stmt.executeQuery(query);

产生以下 SQL 语句

SELECT TRASH_TYPE AS trash_type, COUNT(TRASH_TYPE) AS num FROM trashed WHERE TRASH_DATE LIKE '1-%-2014' GROUP BY TRASH_TYPE

最后这是整个错误信息

java.sql.SQLException: Syntax Error. Encountered " <NAME> "GROUP "" at line 1, column 105.
Was expecting one of:
<EOF> 
"AND" ...
"OR" ...
"ORDER" ...
"LIMIT" ...

at org.relique.jdbc.csv.CsvStatement.executeQuery(CsvStatement.java:331)
at com.lyris.MemberGrowthReport.generateReport(MemberGrowthReport.java:347)
at com.lyris.MemberGrowthReport.requestGenerateReport(MemberGrowthReport.java:167)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
at com.sun.jersey.server.impl.model.method.dispatch.VoidVoidDispatchProvider$VoidVoidMethodInvoker._dispatch(VoidVoidDispatchProvider.java:71)
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1542)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1473)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1419)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1409)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:409)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:540)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:715)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:537)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1085)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1556)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1513)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)

有什么可以说明这个问题的吗?

【问题讨论】:

如果您使用该 SQL 语句并直接在您的数据库上运行它,是否会产生同样的错误? 请查看prepared statements,而不是连接字符串并引入 SQL 注入漏洞。 没有 WHERE 子句是否可以工作?如果没有 GROUP BY 子句,它可以工作吗?如果其中一个有效而另一个无效,您可以更轻松地找到错误。 去掉WHERE子句就更奇怪了。我收到以下错误消息。 。在第 1 行第 78 列遇到“BY”“BY”。期待以下之一:“WHERE”...“ORDER”...“LIMIT”... 【参考方案1】:

这似乎是我使用的 csvjdbc 版本的问题。我用 1.0-18 版本尝试了相同的代码,它运行良好。

有没有人听说过这样的错误(?)?

【讨论】:

以上是关于java.sql.SQLException:语法错误的主要内容,如果未能解决你的问题,请参考以下文章

Error querying database. Cause: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][

报错处理:Cause: java.sql.SQLException: The server time zone value

java连接sqlserver,提示语法错误

启动tomcat时报错了, nested exception is java.sql.SQLException: 调用中无效的参数

java.sql.SQLException: ORA-00604: 递归 SQL 级别 1 出现错误 ORA-01003: 语句未进行语法分析

报错处理:Cause: java.sql.SQLException: The server time zone value