将日期保存到mysql数据库时截断的值不正确

时间:2016-12-27 21:20:18

标签: java mysql database jdbc truncation

我正在尝试将日期保存到mysql数据库。代码运行正常,没有日期实现。

代码:

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

错误:
信息:com.mysql.jdbc.JDBC4PreparedStatement@6f1ea89:INSERT INTO timetableslot(日期,时间,classID,费用)VALUES(STR_TO_DATE('12 / 22/2016','%m /%d /%y'),'12 :00' ,3,11)

严重:com.mysql.jdbc.MysqlDataTruncation:数据截断:截断的日期值不正确:'12 / 22/2016'

0 个答案:

没有答案