Truncated incorrect value when saving a date to a mysql database

167 views Asked by At

I'm trying to save a date to a mysql database. The code runs fine without the date implementation.

Code:

    public boolean saveToDatabase() {

    boolean saved = false;
    Connection c = DBHelperClass.getConnection();

    String template = "INSERT INTO timetableslot (date, time, classID, cost) VALUES (STR_TO_DATE(?, '%m/%d/%y'),?,?,?)";

    if (c != null) {
        try {
            PreparedStatement inserter = c.prepareStatement(template);
            inserter.setString(1, this.getDate());
            inserter.setString(2, this.getTime());
            inserter.setInt(3, this.getClassID());
            inserter.setInt(4, this.getCost());

            System.out.println(inserter);
            inserter.executeUpdate();
            saved = true;
        } catch (SQLException ex) {
            Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    return saved;
}

Error:
Info: com.mysql.jdbc.JDBC4PreparedStatement@6f1ea89: INSERT INTO timetableslot (date, time, classID, cost) VALUES (STR_TO_DATE('12/22/2016', '%m/%d/%y'),'12:00',3,11)

Severe: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Truncated incorrect date value: '12/22/2016'

0

There are 0 answers