我试图在表的现有条目中更新一个字段,但是我得到以下错误消息:
语法错误或访问冲突,来自服务器的消息:“您的SQL语法有错误;请检查与MySQL服务器版本相对应的手册,在第1行的“Update=”Update“,其中用户名=”test“附近使用正确的语法”
我确信语法对准备好的语句是正确的。

setupdate = connection.prepareStatement("UPDATE userdetails SET Update = ? WHERE User_Name = ?");

...
setupdate.setString(1, "friendRequest "+jsonRequest.getString("toadd").toString());
setupdate.setString(2, jsonRequest.getString("user").toString());
System.out.println("executing update");
setupdate.executeUpdate();

...
错误跟踪
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1876)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1098)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1192)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2051)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1680)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1527)
at com.servlet.JSONServlet.doGet(JSONServlet.java:213)
at com.servlet.JSONServlet.doPost(JSONServlet.java:382)
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.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
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:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

最佳答案

更新是一个关键字,因此您需要将其转义

UPDATE userdetails SET `Update` = ? WHERE User_Name = ?"

关于java - 用准备好的语句将字段更新到php数据库,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22483948/

10-11 15:26