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的参数