简体   繁体   English

为什么会出现“ com.mysql.jdbc.MysqlDataTruncation:数据截断:不正确的datetime值:”错误?

[英]Why do I get a “com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value:” error?

I am trying to set the the created_at column time created_at DATETIME NOT NULL in my table on OPENSHIFT to my local time of Kopenhagen . 我试图将我在OPENSHIFT上的表中的created_at列时间created_at DATETIME NOT NULL OPENSHIFT为我的Kopenhagen时间。 How can I set the time in the openshift server to my localtime? 如何将openshift服务器中的时间设置为本地时间? At the moment I am getting this error com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value: '16.05.2015 22:28:13' for column 'created_at' at row 1 at this line prep.executeQuery(); 目前,我收到此错误com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value: '16.05.2015 22:28:13' for column 'created_at' at row 1此行com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value: '16.05.2015 22:28:13' for column 'created_at' at row 1 prep.executeQuery();

I appreciate any help. 感谢您的帮助。

stt.execute("CREATE TABLE IF NOT EXISTS bus"
                    + "(id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,"
                    + "mac VARCHAR(30) NOT NULL UNIQUE,"
                    + "route int(11) NOT NULL,"
                    + "latitude FLOAT(10,6) NOT NULL,"
                    + "longitude FLOAT(10,6) NOT NULL,"
                    + "created_at DATETIME NOT NULL )");

insert_into_table method: insert_into_table方法:

private void insert_into_table(String macD, int routeD, double latD,
        double longD, Connection con) throws SQLException {

    Calendar calendar = new GregorianCalendar();
    TimeZone timeZone = TimeZone.getTimeZone("Europe/Berlin");
    calendar.setTimeZone(timeZone);
    String time = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss")
            .format(calendar.getTime());

    PreparedStatement prep = con
            .prepareStatement("REPLACE INTO bus(mac, route, latitude, longitude, created_at)"
                    + "VALUES( ?, ?, ? , ?,? )");
    prep.setString(1, macD);
    prep.setInt(2, routeD);
    prep.setDouble(3, latD);
    prep.setDouble(4, longD);
    prep.setString(5, time);
    prep.executeQuery();
}

Clearly, the default date/time format on your application computer is not recognized on the MySQL server. 显然,MySQL服务器无法识别应用程序计算机上的默认日期/时间格式。 The easiest fix is to use the database for setting the time. 最简单的解决方法是使用数据库设置时间。

One way is: 一种方法是:

PreparedStatement prep = con
         .prepareStatement("REPLACE INTO bus(mac, route, latitude, longitude, created_at)"
                 + "VALUES( ?, ?, ? , ?, now())");

A better way is to define created_at so it has a default value of the insertion time. 更好的方法是定义created_at ,使其具有插入时间的默认值。 Then you can leave it out of the replace / insert altogether. 然后,您可以将其完全排除在replace / insert This process is explained here. 在此说明此过程 Some details depend on the version of MySQL that you are using. 一些细节取决于您使用的MySQL版本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 com.mysql.jdbc.MysqlDataTruncation:数据截断:不正确的日期时间值:'' - com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value: ' ' com.mysql.jdbc.MysqlDataTruncation:数据截断:日期值不正确 - com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect date value 错误:com.mysql.jdbc.MysqlDataTruncation:数据截断:截断了不正确的DOUBLE值:通过删除 - Error:com.mysql.jdbc.MysqlDataTruncation: Data truncation: Truncated incorrect DOUBLE value: by Delete com.mysql.jdbc.MysqlDataTruncation:数据截断:错误的日期时间值:第1行的'lastchange'列的'0000-00-00 00:00:00' - com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value: '0000-00-00 00:00:00' for column 'lastchange' at row 1 com.mysql.jdbc.MysqlDataTruncation:数据截断:截断了不正确的DOUBLE值:'q' - com.mysql.jdbc.MysqlDataTruncation: Data truncation: Truncated incorrect DOUBLE value: 'q' com.mysql.jdbc.MysqlDataTruncation:数据截断:截断了不正确的DOUBLE值:',3' - com.mysql.jdbc.MysqlDataTruncation: Data truncation: Truncated incorrect DOUBLE value: ',3' com.mysql.jdbc.MysqlDataTruncation:数据截断:错误的日期时间值:第1行“ INVOICE_DATE”列的“ Mon Feb 11 00:00:00 IST 2013” - com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value: 'Mon Feb 11 00:00:00 IST 2013' for column 'INVOICE_DATE' at row 1 “ com.mysql.jdbc.MysqlDataTruncation:数据截断:超出范围值”,按位“或” - “com.mysql.jdbc.MysqlDataTruncation: Data truncation: Out of range value” with bitwise 'OR' com.mysql.jdbc.MysqlDataTruncation:数据截断:第1行的列'DATE'的数据太长 - com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'DATE' at row 1 com.mysql.jdbc.MysqlDataTruncation:数据截断:数据太长以至于列“名称” - com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'name'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM