在做一个博客系统的过程中,使用连接池连接数据库时出现问题。使用的是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”删除,再次运行程序,一切恢复正常。

转载于:https://www.cnblogs.com/chenz/articles/1841538.html

连接数据库报“You have an error in your SQL syntax”异常的解决相关推荐

  1. SQL查询1064报错 [ERR] 1064 - You have an error in your SQL syntax; check the manual.......

    MySQL建表出现1064问题问题 SQL语句 DROP DATABASE IF EXISTS bookstore; DROP DATABASE bookstore; USE bookstore; C ...

  2. centos7 mysql 1064_【mysql报错】1064 - You have an error in your SQL syntax;

    mysql 报错1064 - You have an error in your SQL syntax;https://www.cndba.cn/debimeng/article/3198 数据库版本 ...

  3. 使用mybatis框架分页插件报错### Cause: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax;

    报错信息如下: ### Cause: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the ...

  4. MySQL修改密码报错:ERROR 1064 (42000): You have an error in your SQL syntax

    问题描述: 用"set passwo for -"修改密码的时候报错: ERROR 1064 (42000): You have an error in your SQL synt ...

  5. mysql8设置用户权限报错You have an error in your SQL syntax;right syntax to use near ‘IDENTIFIED BY

    mysql 8 设置用户权限命令和之前不一样 之前: grant all privileges on *.* to 'myuser'@'%' identified by 'mypassword' wi ...

  6. VS报错:fatal error LNK1104: 无法打开文件“kernel32.lib”解决办法

    VS报错:fatal error LNK1104: 无法打开文件"kernel32.lib"解决办法 问题所在 解决方案一 解决方案二 问题所在 我使用VS是运行Fortran程序 ...

  7. mysql报错系列持续整理1064 - You have an error in your SQL syntax;...near...

    前言 怎么又是这个错,感觉似曾相识之前遇到过多次了.是的这就是mysql语句报错1064 - You have an error in your SQL .syntax;...near...感觉每次不 ...

  8. Django >python manage.py migrate报错:(1064, “You have an error in your SQL syntax

    执行如下命令时报错(迁移数据库和表结构): python manage.py makemigrations python manage.py migrate 报错信息: django.db.migra ...

  9. 创建存储过程报错:1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQ

    自己写了一个创建存储过程的语句: create procedure abcd() begin       select * from area; end ; 语法什么的都没有错误,但是提示报错:106 ...

最新文章

  1. PPIO 商业化架构解析
  2. 人工智能实践:TensorFlow笔记学习(一)—— 人工智能概述
  3. 启动MySQL出错: Starting MySQL….. ERROR! The server quit with
  4. Linux之内存描述符mm_struct
  5. reciprocity
  6. java中的==、equals()、hashCode()源码分析
  7. uniapp添加网站favicon文件
  8. 服务:OracleDBConsoleorcl [Agent process exited abnormally during initialization]
  9. 生成注释_SOLIDWORKS DimXpert 自动生成注释
  10. 留给字节跳动的时间可能不多了!
  11. java 修改字体大小
  12. 如何从“人肉运维”升级为“智能运维”?
  13. python爬取网站大数据_[Python]爬虫抓取大数据职位数据
  14. Dilated Convolution(空洞卷积、膨胀卷积)详解
  15. VOLTE信令流程-IMS注册篇(五)
  16. layui框架——弹出层layer
  17. 用计算机打课程表,怎么用电脑做课程表
  18. Qt:Label插入图片
  19. 致大学计算机老师的一封信,致大学老师的一封信
  20. unity3d学习笔记-动画(2.控制动画与Animator Controller)

热门文章

  1. 干货|代码安全审计权威指南(附下载地址)
  2. python科学计算笔记(十二)pandas的resample采样
  3. 地理数据库 (geodatabase) 的架构
  4. Python CSV 中查找指定字符串
  5. 阿里云云效功能升级,支持快速创建多个应用的独立测试环境
  6. “报复黑客”:可行的战略还是重大的风险?
  7. 《深入PHP:面向对象、模式与实践》(二)
  8. Lucene6.0 创建索引及查询text简单实例
  9. CENTOS 内存释放
  10. 《C语言及程序设计》实践参考——乘法口诀表