oracle数据库 时间 TIMESTAMP(6)这是什么类型啊 怎么也插不进数据

oracle数据库 时间 TIMESTAMP(6)这是什么类型啊 怎么也插不进数据,第1张

时间类型,参数6指的是表示秒的数字的小数点右边可以存储6位数字,最多9位。解决方法如下:

1、时间戳的概念:它是一种时间表示方式,定义为从格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数。

2、在oracle使用时间戳,一般都是为了方便计算时间差的,要知道oracle中的date类型想减是不能友好的得到时间的差值的。所以使用时间戳来得到两个时间差。

3、首先来看oracle中如何获得当前时间的时间戳,用当前时间减去计算机元年后再经过换算得到了的从1970年来到当前时间的时间戳,单位是微妙。

4、另外oracle提供了另一种便捷得到两个时间差的方式,那就是使用timestamp数据类型,它类似date类型,但是存储的时间更为精确,显示的格式:yyyy-mm-dd hh24:mi:ssff AM,其中ff是小数秒。

5、timestamp类型的时间差可读性也比上面那种好一些,可以直观看出两个时间差。

在Mysql数据库中使用DATETIME类型来存储时间,使用JDBC中读取这个字段的时候,应该使用 ResultSetgetTimestamp(),这样会得到一个javasqlTimestamp类型的数据。在这里既不能使用 ResultSetgetDate(),也不能使用ResultSetgetTime(),因为前者不包括time数据,后者不包括date数据。

但是在使用ResultSetgetTimestamp()时也不是完全安全的,例如,当数据库中的TIMESTAMP类型的字段值为 '0000-00-00 00:00:00'时,使用此方法进行读取,会抛出异常:Cannot convert value '0000-00-00 00:00:00' from column 1 to TIMESTAMP,这是因为JDBC不能将'0000-00-00 00:00:00'转化为一个为一个javasqlTimestamp,在Java中,想创建一个javautilDate,使其值为 '0000-00-00'也是不可能的,最古老的日期应该是'0001-01-01 00:00:00'。

那么在程序中该怎么办捏? 解决方案在这里:

Datetimes with all-zero components (0000-00-00 ) — These values can not be represented reliably in Java Connector/J 30x always converted them to NULL when being read from a ResultSet

Connector/J 31 throws an exception by default when these values are encountered as this is the most correct behavior according to the JDBC and SQL standards This behavior can be modified using the zeroDateTimeBehavior configuration property The allowable values are:

exception (the default), which throws an SQLException with an SQLState of S1009

convertToNull, which returns NULL instead of the date

round, which rounds the date to the nearest closest value which is 0001-01-01

Starting with Connector/J 317, ResultSetgetString() can be decoupled from this behavior via noDatetimeStringSync=true (the default value is false) so that you can retrieve the unaltered all-zero value as a String It should be noted that this also precludes using any time zone conversions, therefore the driver will not allow you to enable noDatetimeStringSync and useTimezone at the same time

所以,在JDBC URL中加入zeroDateTimeBehavior信息,既可以解决:

String url = "jdbc:mysql://101495180:3306/testrelaxAutoCommit=true&zeroDateTimeBehavior=convertToNull";

以上就是关于oracle数据库 时间 TIMESTAMP(6)这是什么类型啊 怎么也插不进数据全部的内容,包括:oracle数据库 时间 TIMESTAMP(6)这是什么类型啊 怎么也插不进数据、java.sql.SQLException: Cannot convert value '0000-00-00 00:00:00' from column 7 to TIMESTAMP.、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/9672286.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-30
下一篇 2023-04-30

发表评论

登录后才能评论

评论列表(0条)

保存