SQL 查询在 phpMyAdmin 中有效,但在 Java 中无效
Posted
技术标签:
【中文标题】SQL 查询在 phpMyAdmin 中有效,但在 Java 中无效【英文标题】:SQL Query Works in phpMyAdmin but not in Java 【发布时间】:2013-04-03 23:21:12 【问题描述】:我有以下 SQL 查询:
SELECT `Activity`.`userid` , `Goal`.`goalid` , `Goal`.`activityid` , `Goal`.`exercise` , `Exercise`.`name` , `Goal`.`target` , `ActivityData`.`dataid` , `ActivityData`.`current` , MAX( `ActivityData`.`timestamp` ) AS `timestamp`
FROM `Goal`
INNER JOIN `Activity` ON `Activity`.`activityid` = `Goal`.`activityid`
INNER JOIN `Exercise` ON `Exercise`.`exerciseid` = `Goal`.`exercise`
INNER JOIN `ActivityData` ON `ActivityData`.`activityid` = `Goal`.`activityid`
WHERE `Activity`.`userid` =100005545413982
GROUP BY `gymbuddy`.`Goal`.`activityid`;
上面的查询在 phpMyAdmin 中有效,但是当我使用 PreparedStatement 运行它时,会给出以下堆栈跟踪。
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'BY `gymbuddy`.`Goal`.`activityid`' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1053)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4096)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4028)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2490)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2734)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2322)
at com.mathanv.gb.dao.BuddyDao.getAllBuddyGoals(BuddyDao.java:387)
at com.mathanv.gb.controller.BuddyController.doPost(BuddyController.java:95)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
at java.lang.Thread.run(Thread.java:680)
这些是数据库属性:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:8889/gymbuddy?allowMultiQueries=true
不知道为什么会这样!帮忙!
编辑:我正在使用preparedStatement 来生成用户ID。
WHERE `gymbuddy`.`Activity`.`userid` =?
ps.setLong(1, buddy.getUserID());
编辑:带有 PreparedStatement 的(请求)方法。
public JSONObject getAllBuddyGoals(Buddy buddy)
try
PreparedStatement ps = connection.prepareStatement(GET_ALL_BUDDY_GOALS);
ps.setLong(1, buddy.getUserID());
JSONObject response = new JSONObject();
response.put("action", "getAllBuddyGoals");
response.put("success", 1);
response.put("error", 0);
response.put("userid", buddy.getUserID());
JSONArray goals = new JSONArray();
ResultSet rs = ps.executeQuery();
while(rs.next())
JSONObject goal = new JSONObject();
goal.put("goalid", rs.getLong("goalid"));
goal.put("activityid", rs.getLong("activityid"));
goal.put("exercise", rs.getString("name"));
goal.put("target", rs.getDouble("target"));
goal.put("max", rs.getDouble("current"));
goals.add(goal);
response.put("goals", goals);
return response;
catch (SQLException e)
e.printStackTrace();
JSONObject error = new JSONObject();
error.put("action", "getAllBuddyGoals");
error.put("success", 0);
error.put("error", 1);
error.put("error_msg", "error processing request, please contact admin.");
return error;
【问题讨论】:
buddy.getUserID()
返回什么?
一个长数字,例如100005545413982
你能告诉我们preparedStatement的全部代码吗?
是的,当然。在问题中添加它。
【参考方案1】:
您可能需要检查好友 id 值和术语组之间是否存在实际空格。如果你阅读了 Java 抛出的错误:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:
You have an error in your SQL syntax; check the manual
that corresponds to your MySQL server version for the
right syntax to use near 'BY `gymbuddy`.`Goal`.`activityid`' at line 1
Mysql 错误通常发生在错误出现之前。我的猜测是您正在执行的查询在 100005545413982 和 GROUP 之间缺少一些空格,可能是这样的:
WHERE `Activity`.`userid` =100005545413982GROUP BY `gymbuddy`.`Goal`.`activityid`;
【讨论】:
我只是打了自己一巴掌。不敢相信一点点空间可以浪费很多时间哈哈!非常感谢朋友!以上是关于SQL 查询在 phpMyAdmin 中有效,但在 Java 中无效的主要内容,如果未能解决你的问题,请参考以下文章
SQL Sum Query 在 PHPmyAdmin 中有效,但在 Java resutset 中无效
MySQL Query 在 PhpMyAdmin 中有效,但在 JAVA Eclipse 中无效
SQL 查询在通过 phpmyadmin 进行测试时工作正常,但在 PHP PDO 中尝试相同的事情时失败
MySql 查询在 PHPmyadmin 中工作,但在 codeigniter 中不工作