Solve the problem that @InsertProvider stores Chinese characters and the date received by the backend as a string throws an exception

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'


The above is the exception thrown, below I will post the wrong sqlProvider code

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();
    }

This is an error. At first, I didn't pay much attention to the printed sql. After reading a lot of source code, I realized that my date and name are stored as strings, but I did not set a character when executing update. string, but an object, without double quotes. I then put the date and name in a double quotation mark and it was inserted successfully.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324762842&siteId=291194637