Tag Archives: FAQ(57)java.sql.SQLException: Can not issue data manipulation statements with executeQuery().

FAQ 57: java.sql.SQLException: Can not issue data manipulation statements with executeQuery().

Open source software supply chain lighting plan, waiting for you>>>

1. Scenario: Spring MVC + spring

2、Log:

java.sql.SQLException: Can not issue data manipulation statements with executeQuery().
	com.mysql.jdbc.Statement.checkForDml(Statement.java:298)
	com.mysql.jdbc.Statement.executeQuery(Statement.java:750)
	com.User.Dao.UserDaoImpl.add(UserDaoImpl.java:199)
	com.User.Service.UserServiceImpl.add(UserServiceImpl.java:36)
	com.User.Controller.UserController.addUserSave(UserController.java:177)
	sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	java.lang.reflect.Method.invoke(Unknown Source)
	org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
	org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
	org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
	org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
	org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
	org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
	org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
	org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
	org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
	org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
	org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
	org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

Solution:

The reason is: St. executeQuery (“insert into student values (‘” + name +’, ‘”+ age +’, ‘” + introduction +’));

I read an example in the book, and then I started to imitate it. When I found the problem, I found that this sentence was used in other program statements in the book,

Later, after reading several web pages, I realized that this sentence should be like this: St. execute (“insert into student values (‘” + name +’, ‘”+ age +’, ‘” + introduce +’ “);

Conclusion: if your SQL statement is an update statement such as update and insert, you should use the execute () method of statement. If you use the executeQuery () method of statement, there will be an appeal problem