连接数据库报“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”删除,再次运行程序,一切恢复正常。
转载于:https://www.cnblogs.com/chenz/articles/1841538.html
连接数据库报“You have an error in your SQL syntax”异常的解决相关推荐
- 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 ...
- 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 数据库版本 ...
- 使用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 ...
- 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 ...
- 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 ...
- VS报错:fatal error LNK1104: 无法打开文件“kernel32.lib”解决办法
VS报错:fatal error LNK1104: 无法打开文件"kernel32.lib"解决办法 问题所在 解决方案一 解决方案二 问题所在 我使用VS是运行Fortran程序 ...
- mysql报错系列持续整理1064 - You have an error in your SQL syntax;...near...
前言 怎么又是这个错,感觉似曾相识之前遇到过多次了.是的这就是mysql语句报错1064 - You have an error in your SQL .syntax;...near...感觉每次不 ...
- 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 ...
- 创建存储过程报错: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 ...
最新文章
- PPIO 商业化架构解析
- 人工智能实践:TensorFlow笔记学习(一)—— 人工智能概述
- 启动MySQL出错: Starting MySQL….. ERROR! The server quit with
- Linux之内存描述符mm_struct
- reciprocity
- java中的==、equals()、hashCode()源码分析
- uniapp添加网站favicon文件
- 服务:OracleDBConsoleorcl [Agent process exited abnormally during initialization]
- 生成注释_SOLIDWORKS DimXpert 自动生成注释
- 留给字节跳动的时间可能不多了!
- java 修改字体大小
- 如何从“人肉运维”升级为“智能运维”?
- python爬取网站大数据_[Python]爬虫抓取大数据职位数据
- Dilated Convolution(空洞卷积、膨胀卷积)详解
- VOLTE信令流程-IMS注册篇(五)
- layui框架——弹出层layer
- 用计算机打课程表,怎么用电脑做课程表
- Qt:Label插入图片
- 致大学计算机老师的一封信,致大学老师的一封信
- unity3d学习笔记-动画(2.控制动画与Animator Controller)