2014-12-09 93 views
0

下面的代碼是提高語法錯誤異常 我使用Ajax在後臺調用來執行表上的CRUD操作com.mysql.jdbc.exceptions.MySQLSyntaxErrorException同時刪除記錄在MySQL

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) 
+3

你一定的相同的代碼那給異常?因爲在查詢中你使用的是'recordid = 2',但是你得到'USERID = 102'的異常 – Adi 2014-12-09 11:36:35

+0

看起來像ErrorException不是這個代碼 – Jay 2014-12-09 11:37:31

+0

我編輯了代碼@Adi – rajasekhar 2014-12-09 11:38:49

回答

2

您需要在table namewhere條款之間有space

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

目前您sql腳本看起來像

delete from tablenamewhere USERID=102

+0

這工作正常它不是拋出任何例外,但記錄不會被刪除在數據庫中。 – rajasekhar 2014-12-09 12:04:33

+0

謝謝你的工作很好.. – rajasekhar 2014-12-09 12:08:35

1

嘗試改變

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

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

你的表名和where用之間沒有空格一起運行的關鍵詞!

+0

真棒它的工作完美... – rajasekhar 2014-12-09 12:13:34