Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 184|回复: 0

[默认分类] 解决@InsertProvider存汉字和后端以字符串接收的日期抛异常

[复制链接]
  • TA的每日心情
    开心
    2021-12-13 21:45
  • 签到天数: 15 天

    [LV.4]偶尔看看III

    发表于 2018-4-26 10:50:08 | 显示全部楼层 |阅读模式

    1. org.springframework.jdbc.BadSqlGrammarException:
    2. ### Error updating database.  Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column "张璐" in "field list"
    3. ### The error may involve com.example.demo.Repository.EducateRepository.insertEducate-Inline
    4. ### The error occurred while setting parameters
    5. ### SQL: INSERT INTO educate  (createtime, name) VALUES (2018-10-22, 张璐)
    6. ### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column "张璐" in "field list"
    复制代码


    上面是抛出的异常,下面我把错误的sqlProvider代码贴出来
    1. public class EducateProvider {
    2.     private SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
    3.     public String insertEducate(Educate educate) {
    4. //        educate.setBegintime("2018-10-22");
    5.         return new SQL() {
    6.             {
    7.                 INSERT_INTO("educate");
    8.                 if (educate.getBegintime() != null&&!educate.getBegintime().equals("")) {
    9.                     VALUES("begintime",educate.getBegintime().toString() );
    10.                 }
    11.                 if (educate.getCreatetime() != null&&!educate.getCreatetime().equals("")) {
    12.                     VALUES("createtime", "2018-10-22");
    13.                 }
    14.                 if (educate.getDatum() != null&&!educate.getDatum().equals("")){
    15.                     VALUES("datum", educate.getDatum());
    16.                 }
    17.                 if (educate.getEducateState() != null&&!educate.getEducateState().equals("")) {
    18.                     VALUES("educate_state", educate.getEducateState().toString());
    19.                 }
    20.                 if (educate.getEffect() != null&&!educate.getEffect().equals("")) {
    21.                     VALUES("effect", educate.getEffect());
    22.                 }
    23.                 if (educate.getEffectflag() != null) {
    24.                     VALUES("effectFlag", educate.getEffectflag().toString());
    25.                 }
    26.                 if (educate.getEndtime() != null&&!educate.getEndtime().equals("")) {
    27.                     VALUES("endtime", educate.getEndtime());
    28.                 }
    29.                 if (educate.getIsreviewed() != null) {
    30.                     VALUES("isreviewed", educate.getIsreviewed().toString());
    31.                 }
    32.                 if (educate.getMonth() != null&&!educate.getMonth().equals("")) {
    33.                     VALUES("month", educate.getMonth().toString());
    34.                 }
    35.                 if (educate.getName() != null&&!educate.getName().equals("")) {
    36.                     VALUES("name", "张璐");
    37.                 }
    38.                 if (educate.getPurpose() != null&&!educate.getPurpose().equals("")) {
    39.                     VALUES("purpose", educate.getPurpose());
    40.                 }
    41.                 if (educate.getRemark() != null&&!educate.getRemark().equals("")) {
    42.                     VALUES("remark", educate.getRemark());
    43.                 }
    44.                 if (educate.getStudent() != null&&!educate.getStudent().equals("")) {
    45.                     VALUES("student",educate.getStudent() );
    46.                 }
    47.                 if (educate.getSummarize() != null&&!educate.getSummarize().equals("")) {
    48.                     VALUES("summarize", educate.getSummarize());
    49.                 }
    50.                 if (educate.getTeacher() != null&&!educate.getTeacher().equals("")) {
    51.                     VALUES("teacher", educate.getTeacher());
    52.                 }
    53.             }
    54.         }.toString();
    55.     }
    复制代码


    这就报错了,起初对这个打印出来的的sql没太在意,看了很多源代码,后来反应过来了,我的日期和名字是以字符串存储的,可是执行update的时候没有set进去一个字符串,而是一个对象,没有双引号。我接着把日期和名字在外面再套一个双引号就成功插入了。所以我们一般用#{}来接受sql的参数
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-5-16 17:27 , Processed in 0.360987 second(s), 37 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表