0
votes

I am getting the following error in my code:

org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad 
SQL grammar [insert into bulletins (date, name, subject, note, approved) values 
(?, ?, ?, ?, ?)]; nested exception is com.mysql.jdbc.exceptions.MySQLSyntaxError 
Exception: Unknown column 'date' in 'field list'

This line is in my Spring controller.

bulletinDAO.writeBulletin(bulletin);

The actual place in my DAO class where I'm trying to write using Hibernate.

public void writeBulletin(Bulletin bulletin) {
    try {
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        session.save(bulletin);
        tx.commit();
    } catch (Exception e) {
        System.out.println(e.toString());
    }
}

Here is my model class.

    @Entity
    @Table(name="login")
    public class Bulletin {
        @Id
        @Column(name="id")
        @GeneratedValue
        private int id;

        @Column(name="bulletin_date")
        private String date;

        @Column(name="name")
        private String name;

        @Column(name="subject")
        private String subject;

        @Column(name="note")
        private String note;

        @Column(name="approved")
        private boolean approved;

        // Getters and setters follow
}

Finally, here is the layout of the table.

+---------------+---------------+------+-----+---------+----------------+
| Field         | Type          | Null | Key | Default | Extra          |
+---------------+---------------+------+-----+---------+----------------+
| id            | int(11)       | NO   | PRI | NULL    | auto_increment |
| bulletin_date | varchar(10)   | YES  |     | NULL    |                |
| name          | varchar(30)   | YES  |     | NULL    |                |
| subject       | varchar(50)   | YES  |     | NULL    |                |
| note          | varchar(2500) | YES  |     | NULL    |                |
| approved      | tinyint(1)    | YES  |     | NULL    |                |
+---------------+---------------+------+-----+---------+----------------+
2
It's bulletin_date, isn't it? - PM 77-1

2 Answers

0
votes

There must be something wrong with your getters and setters.

I would recommend changing the property name from date to bulletinDate. And then set & get it correctly...

 @Column(name="bulletin_date")
 private String bulletinDate;

 public String getBulletinDate() {
    return bulletinDate;
 }
 public void setBulletinDate(String bulletin_date) {
    this.bulletinDate = bulletin_date;
 }
0
votes

Your issue is here [insert into bulletins (date, name, subject, note, approved)]

Whereas you need bullletin_date.

From experience i needed to rebuild the project completely to ensure that right column name is referenced.

Clear your project cache and rebuild it.

Let me know how you go and i'll help further if it doesn't help.