解决@InsertProvider存汉字和后端以字符串接收的日期抛异常

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


上面是抛出的异常,下面我把错误的sqlProvider代码贴出来

public class EducateProvider {
    private SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
    public String insertEducate(Educate educate) {
//        educate.setBegintime("2018-10-22");
        return new SQL() {
            {
                INSERT_INTO("educate");
                if (educate.getBegintime() != null&&!educate.getBegintime().equals("")) {
                    VALUES("begintime",educate.getBegintime().toString() );
                }
                if (educate.getCreatetime() != null&&!educate.getCreatetime().equals("")) {
                    VALUES("createtime", "2018-10-22");
                }
                if (educate.getDatum() != null&&!educate.getDatum().equals("")){
                    VALUES("datum", educate.getDatum());
                }
                if (educate.getEducateState() != null&&!educate.getEducateState().equals("")) {
                    VALUES("educate_state", educate.getEducateState().toString());
                }
                if (educate.getEffect() != null&&!educate.getEffect().equals("")) {
                    VALUES("effect", educate.getEffect());
                }
                if (educate.getEffectflag() != null) {
                    VALUES("effectFlag", educate.getEffectflag().toString());
                }
                if (educate.getEndtime() != null&&!educate.getEndtime().equals("")) {
                    VALUES("endtime", educate.getEndtime());
                }
                if (educate.getIsreviewed() != null) {
                    VALUES("isreviewed", educate.getIsreviewed().toString());
                }
                if (educate.getMonth() != null&&!educate.getMonth().equals("")) {
                    VALUES("month", educate.getMonth().toString());
                }
                if (educate.getName() != null&&!educate.getName().equals("")) {
                    VALUES("name", "张璐");
                }
                if (educate.getPurpose() != null&&!educate.getPurpose().equals("")) {
                    VALUES("purpose", educate.getPurpose());
                }
                if (educate.getRemark() != null&&!educate.getRemark().equals("")) {
                    VALUES("remark", educate.getRemark());
                }
                if (educate.getStudent() != null&&!educate.getStudent().equals("")) {
                    VALUES("student",educate.getStudent() );
                }
                if (educate.getSummarize() != null&&!educate.getSummarize().equals("")) {
                    VALUES("summarize", educate.getSummarize());
                }
                if (educate.getTeacher() != null&&!educate.getTeacher().equals("")) {
                    VALUES("teacher", educate.getTeacher());
                }
            }
        }.toString();
    }

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值