连接数据库报“You have an error in your SQL syntax”异常的解决
在做一个博客系统的过程中,使用连接池连接数据库时出现问题。使用的是Mysql数据库,向数据库中插入数据时出错,错误提示如下:
com.mysql.jdbc.exceptions.jdbc4.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 '?,?,?,now())' at line 1at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)at java.lang.reflect.Constructor.newInstance(Constructor.java:513)at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)at com.mysql.jdbc.Util.getInstance(Util.java:384)at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3566)at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3498)at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959)at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2113)at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2562)at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1664)at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1583)at org.apache.tomcat.dbcp.dbcp.DelegatingStatement.executeUpdate(DelegatingStatement.java:228)at org.apache.tomcat.dbcp.dbcp.DelegatingStatement.executeUpdate(DelegatingStatement.java:228)at com.chenzheng.blog.BlogServlet.doPost(BlogServlet.java:52)at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:861)at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1584)at java.lang.Thread.run(Thread.java:619)
涉及的程序代码如下:
DataSource ds = null; try { Context context = new InitialContext(); ds = (DataSource) context.lookup("java:/comp/env/jdbc/mysqlds"); } catch (NamingException e) { System.out.println("获取数据源时出错!"); } try { Connection conn = ds.getConnection(); String insertBlog = "insert into blog(title,content,category_id,createdtime) values(?,?,?,now())"; PreparedStatement pstmt = conn.prepareStatement(insertBlog); pstmt.setString(1, title); pstmt.setString(2, content); pstmt.setInt(3, Integer.parseInt(categoryId)); pstmt.executeUpdate(insertBlog); } catch (SQLException e) { e.printStackTrace(); }
运行环境应该没错,tomcat中的数据连接池的配置文件也正确,在lib中也加入了驱动文件mysql-connector-java-5.1.13-bin.jar。
检查代码,将sql语句中的“?”替换为具体的值,不使用动态传输,运行后发现sql语句可以正常执行,数据可以插入到表中;将sql语句中的values值重新换成“?”,
程序又不能正常插入数据到数据库,报同样的错误。
查阅jdk1.6API文档中的executeUpdate()方法,在文档中executeUpdate()方法括号中并无参数,试着将程序代码中第16行executeUpdate()方法中的参数“insertBlog”删除,再次运行程序,一切恢复正常。