删除mysql中的记录时出现com.mysql.jdbc.exceptions.MySQLSyntaxErrorException

时间:2014-12-09 11:33:05

标签: java mysql jdbc

以下代码引发语法错误异常 我在后端使用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);
        }
    }


}

在上面的代码中我只编写了删除方法,它抛出了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)

2 个答案:

答案 0 :(得分:2)

您需要在spacetable name条款之间设置where

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

目前您的sql脚本看起来像

delete from tablenamewhere USERID=102

答案 1 :(得分:1)

尝试更改

        PreparedStatement preparedStatement = connection
                .prepareStatement("delete from " + table
                        + "where USERID=?");

        PreparedStatement preparedStatement = connection
                .prepareStatement("delete from " + table
                        + " where USERID=?");

您的表名和关键字where一起运行,没有空格!