gpt4 book ai didi

java - com.mysql.jdbc.exceptions.MySQLSyntaxErrorException 删除 mysql 中的记录时

转载 作者:太空宇宙 更新时间:2023-11-03 12:04:18 25 4
gpt4 key购买 nike

以下代码引发语法错误异常我在后端使用 Ajax 调用对表进行 CRUD 操作

public class CrudDao {
private Connection connection;
private String table;

public CrudDao(Connection con, String table) {
this.connection = con;
this.table = table;

}

public void deleteUser(int RECORDID) {
try {

PreparedStatement preparedStatement = connection
.prepareStatement("delete from " + table
+ "where USERID=?");
preparedStatement.setInt(1, RECORDID);
preparedStatement.executeUpdate();
} catch (SQLException e) {
System.out.println(e);
}
}


}

在上面的代码中,我只写了 delete 方法,它抛出 com.mysql.jdbc.exceptions.MySQLSyntaxErrorException

异常的踪迹是

com.mysql.jdbc.exceptions.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 'USERID=102' at line 1
{"Result":"OK"}
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2870)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1573)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1665)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3176)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1153)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1404)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1318)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1303)
at com.symp.CrudDao.deleteUser(CrudDao.java:33)
at com.symp.CRUDController.service(CRUDController.java:82)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)

最佳答案

您需要在表名 和您的where 子句之间有空格

"delete from " + table + " where USERID=?"

目前你的sql脚本看起来像

从表名中删除 USERID=102

关于java - com.mysql.jdbc.exceptions.MySQLSyntaxErrorException 删除 mysql 中的记录时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27377951/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com