2015-10-07 72 views
1

我試圖更新MySQL數據庫使用JDBC模板更新查詢春季JDBC模板更新查詢錯誤

我的查詢

String query = "update request set regions=?, requesttype=?, requestorigin=?, lineofbusiness=?, destinationenvironment=?, release=?, workrequest=?," 
        + " spmid=?, requestingteam=? where requestid="+request.getRequestId()+";"; 
      System.out.println(query); 
      Object[] params = {request.getRegions(), request.getRequestType(), request.getRequestOrigin(), 
        request.getLineOfBusiness(), 
        request.getDestinationEnvironment(), 
        request.getReleaseValue(), request.getWorkRequest(), 
        request.getSpmId(), request.getRequestingTeam() 
        }; 
      int[] types = { Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, 
        Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, 
        Types.VARCHAR }; 
      return jdbcTemplate.update(query, params, types); 

當我運行此查詢它顯示了Mysql的語法錯誤。我無法找到
我錯了。

堆棧跟蹤

SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/TDM] threw exception [Request processing failed; nested exception is org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [update request set regions=?, requesttype=?, requestorigin=?, lineofbusiness=?, destinationenvironment=?, release=?, workrequest=?, spmid=?, requestingteam=? where requestid=319;]; nested exception is 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 'release='Search and Load', workrequest='zxZ', spmid='zxXZxZ', requestingteam='Se' at line 1] with root cause 
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 'release='Search and Load', workrequest='zxZ', spmid='zxXZxZ', requestingteam='Se' at line 1 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2941) 
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1623) 
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1715) 
    at com.mysql.jdbc.Connection.execSQL(Connection.java:3249) 
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1268) 
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1541) 
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1455) 
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1440) 
    at org.springframework.jdbc.core.JdbcTemplate$2.doInPreparedStatement(JdbcTemplate.java:873) 
    at org.springframework.jdbc.core.JdbcTemplate$2.doInPreparedStatement(JdbcTemplate.java:866) 
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:629) 
    at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:866) 
    at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:927) 
    at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:932) 
    at org.verizon.tdm.dao.impl.RequestDaoImpl.updateRequest(RequestDaoImpl.java:62) 
    at org.verizon.tdm.service.impl.RequestServiceImpl.updateRequestDetails(RequestServiceImpl.java:27) 
    at org.verizon.tdm.controller.RequestDetailsController.saveRequestAjaxPage(RequestDetailsController.java:53) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:497) 
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221) 
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137) 
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:111) 
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:806) 
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:729) 
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) 
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959) 
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893) 
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) 
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:644) 
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:725) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:301) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:136) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:74) 
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516) 
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1015) 
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:652) 
    at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222) 
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1575) 
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1533) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 
    at java.lang.Thread.run(Thread.java:745) 

我的架構

***Field Type Null Key Default Extra*** 
requestid int(11) NO PRI NULL auto_increment 
regions varchar(250) YES  NULL  
requesttype varchar(95) YES  NULL  
requestorigin varchar(25) YES  NULL  
lineofbusiness varchar(250) YES  NULL  
destinationenvironment varchar(250) YES  NULL  
release varchar(250) YES  NULL  
workrequest varchar(250) YES  NULL  
spmid varchar(250) YES  NULL  
requestingteam varchar(250) YES  NULL  
datapoints varchar(500) YES  NULL  
status enum('SUBMITTED','SERVICED','DRAFT','INPROGRESS','FAILED') YES  NULL  
+0

答: 我不是在DB很好..其實列版本是MySQL中的關鍵字..這就是爲什麼它被拋出的異常.. :) – Mahi

回答

2

基於此錯誤消息:

com.mysql.jdbc.exceptions.MySQLSyntaxErrorException:你有你的SQL語法錯誤;檢查與您的MySQL服務器版本相對應的手冊,以便在第1行的'release ='Search and Load',workrequest ='zxZ',spmid ='zxXZxZ',requestsam ='Se'附近使用正確的語法

該問題似乎與release列有關。 你確定該列存在嗎? (正確拼寫,在名稱正確/小寫)

UPDATE

事實證明,release是MySQL中的關鍵字。 您需要引用使用反引號的那場,就像這樣:

String query = "update request set regions=?, `release`=?, workrequest=?," 

不相關的問題, 但就像你注入到設定值是多少?參數,在條件中爲request.getRequestId()執行相同的操作,而不是字符串連接。這樣它將以安全的方式注入。

也就是說,SQL字符串的結尾應該是這樣的:

... where requestid= ? 

,並在參數就像您設置,在paramstypes其他值添加request.getRequestId()

2

檢查是否加引號,解決問題嗎?

"update `request` set `regions`=?, `requesttype`=?, `requestorigin`=?, `lineofbusiness`=?, `destinationenvironment`=?, `release`=?, `workrequest`=?," 
        + " `spmid`=?, `requestingteam`=? where `requestid`=" 
+1

嘿它會工作:),但很抱歉,我不能接受兩個答案..反正實際的原因是由於使用關鍵字作爲列名 – Mahi

+0

沒問題,你可以upvote答案:)。很好,你已經解決了你的問題。 :) –

0

爲什麼你還沒有參數化查詢中的requestId?假設的requestId爲整數類型,代碼可以被重新寫入此:

String query = "update request set regions=?, requesttype=?, requestorigin=?, lineofbusiness=?, destinationenvironment=?, release=?, workrequest=?," 
        + " spmid=?, requestingteam=? where requestid=?"; 
System.out.println(query); 
Object[] params = {request.getRegions(), request.getRequestType(), request.getRequestOrigin(), 
     request.getLineOfBusiness(), 
     request.getDestinationEnvironment(), 
     request.getReleaseValue(), request.getWorkRequest(), 
     request.getSpmId(), request.getRequestingTeam(), 
     request.getRequestId()}; 
int[] types = { Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, 
     Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, 
     Types.VARCHAR, Types.INTEGER }; 
return jdbcTemplate.update(query, params, types); 

讓我知道這對你的作品。

+0

仍然我得到相同的錯誤 – Mahi

0
request.getSpmId(), request.getRequestingTeam(), 

您在Object數組末尾放置逗號。

+0

不,我有一個元素添加request.getRequestingTeam() – Mahi

+0

後,但你已經給9個值9個佔位符。我對嗎? – jcool

+0

多數民衆贊成在一個很好的..但這不是一個問題..我已經更新我的代碼基於janos的建議..仍然會拋出異常 – Mahi