简体   繁体   English

在Java中以毫秒精度将Instant datetime插入MySql数据库

[英]Insert Instant datetime into MySql database with millisecond precision in java

I am using mysql 5.7.x version. 我使用的是mysql 5.7.x版本。 Also i am using java 8. I am trying to insert java.time.instant current datetime in millisecond precision into my mysql database from java code. 我也在使用java 8.我试图将java.time.instant当前日期时间以毫秒精度从java代码插入到我的mysql数据库中。 For that I am using preparedstatement . 为此,我正在使用preparedstatement

The table in my database is: 我的数据库中的表是:

CREATE TABLE `test_table` (
`id` INT NOT NULL AUTO_INCREMENT,
`timestamp` TIMESTAMP(3) NOT NULL,
PRIMARY KEY (`id`));

My java code to insert is: 我要插入的java代码是:

    Instant instant = Instant.now().truncatedTo(ChronoUnit.MILLIS);

    try (Connection conn = DbConnection.getCon();
            PreparedStatement ps = conn.prepareStatement("INSERT INTO test_table (timestamp) VALUES (?)");) {
        ps.setTimestamp(1, Timestamp.from(instant));
        ps.executeUpdate();
        LOG.info("Instant: {} and long: {}", instant, instant.toEpochMilli());
    } catch (SQLException ex) {
        LOG.error(ex.getMessage());
    }

From my log, I can see instant with milliesecond as: 2019-07-30T10:52:34.865Z. 从我的日志中,我可以看到毫秒的瞬间:2019-07-30T10:52:34.865Z。 But in my mysql database it becomes: 2019-07-30 10:52:34.000Z 但在我的mysql数据库中它变成:2019-07-30 10:52:34.000Z

I have searched so many questions and answers in stack but none seems to work for me. 我已经在堆栈中搜索了这么多问题和答案,但似乎没有一个对我有用。

Update 1: 更新1:

I tried using setObject as: 我尝试使用setObject作为:

ps.setObject(1, Timestamp.from(instant));

But still same result. 但结果仍然相同。 Cannot retrieve the milliseconds in database. 无法检索数据库中的毫秒数。

MySQL has FROM_UNIXTIME() function that can take long values inside and returns a representation of the unix_timestamp in 'YYYY-MM-DD hh:mm:ss' or 'YYYYMMDDhhmmss.uuuuuu' format. MySQL具有FROM_UNIXTIME()函数,该函数可以在内部获取long值并以'YYYY-MM-DD hh:mm:ss'或'YYYYMMDDhhmmss.uuuuuu'格式返回unix_timestamp的表示。 Reference from their manual here: FROM_UNIXTIME() 在这里参考他们的手册: FROM_UNIXTIME()

Since I have milliseconds since epoch, thus I have to use 因为我从纪元开始有几毫秒,所以我必须使用

FROM_UNIXTIME(instant.toEpochMilli() * 0.001) FROM_UNIXTIME(instant.toEpochMilli()* 0.001)

in my insert statement. 在我的插入声明中。

Yes nothing needs to be changed in database. 是的,无需在数据库中更改任何内容。 The only things that are changed are in java code which are setObject() function used for preparedStatement , passing instant.toEpochMilli() as the argument there and finally use of FROM_UNIXTIME() function inside insert statement. 被唯一改变的是Java代码的setObject()用于函数preparedStatement ,路过instant.toEpochMilli()作为参数有最后使用的FROM_UNIXTIME() insert语句里面的功能。

My final java code looks something like this: 我的最终java代码看起来像这样:

Instant instant = Instant.now().truncatedTo(ChronoUnit.MILLIS);

    try (Connection conn = DbConnection.getCon();
            PreparedStatement ps = conn.prepareStatement("INSERT INTO test_table (timestamp) VALUES (FROM_UNIXTIME(?*0.001))");) {
        ps.setObject(1, instant.toEpochMilli());
        ps.executeUpdate();
        LOG.info("Instant: {} and long: {}", instant, instant.toEpochMilli());
    } catch (SQLException ex) {
        LOG.error(ex.getMessage());
    }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM