简体   繁体   English

JDBC模板更新查询错误

[英]JDBC template update query error

I am trying to update mysql DB with jdbc template update query in Spring 我正在尝试在春季使用jdbc模板更新查询更新mysql数据库

My Query 我的查询

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);  

When i run this query it shows Mysql Grammar error. 当我运行此查询时,它显示Mysql Grammar错误。 I couldnt able to find 我找不到
where i went wrong. 我哪里出错了。

Stack Trace 堆栈跟踪

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)  

My Schema 我的架构

***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    

Based on this error message: 根据此错误消息:

com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax; com.mysql.jdbc.exceptions.MySQLSyntaxErrorException:您的SQL语法有错误; 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 检查与MySQL服务器版本相对应的手册,以在第1行的'release ='Search and Load',workrequest ='zxZ',spmid ='zxXZxZ',requestingteam ='Se'附近使用正确的语法

The problem seems to be with the release column. 问题似乎出在release专栏上。 Are you sure the column exists? 您确定该列存在吗? (Correct spelling, correct upper/lower case in the name.) (正确的拼写,名称中的大小写正确。)

UPDATE 更新

As it turns out, release is a keyword in MySQL. 事实证明, release是MySQL中的关键字。 You need to quote that field using backticks, like this: 您需要使用反引号将该字段引号,例如:

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

Not related to your problem, but just like you inject the values to set with ? 与您的问题无关,但就像您注入要使用设置的值一样? parameters, do the same for request.getRequestId() in the condition, instead of string concatenation. 参数,对条件中的request.getRequestId()进行相同的操作,而不是字符串串联。 That way it will be injected in a type safe way. 这样,它将以类型安全的方式注入。

That is, the end of the SQL string should look like: 也就是说,SQL字符串的结尾应如下所示:

... where requestid= ?

and add request.getRequestId() in the parameters just like the other values that you set, in params and types . 并在参数中添加request.getRequestId() ,就像您设置的其他值一样,在paramstypes

Check whether by putting quotes, does it solve the problem? 检查是否通过引号解决了问题?

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

Why don't you also parameterize the requestId in your query? 为什么不在查询中也参数化requestId? Assuming the requestId is of type INTEGER, the code can be re-written to this: 假设requestId的类型为INTEGER,则可以将代码重写为:

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);  

Let me know if this works for you. 让我知道这是否适合您。

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

您将逗号放在对象数组的末尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM