`
zhaomengsen
  • 浏览: 207335 次
  • 性别: Icon_minigender_1
  • 来自: 河北
社区版块
存档分类
最新评论

zeroDateTimeBehavior=convertToNull

阅读更多
Cannot convert value '0000-00-00 00:00:00' from column 1 to TIMESTAMP
2008年09月22日 星期一 11:13

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

但是在使用ResultSet.getTimestamp()时也不是完全安全的,例如,当数据库中的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'转化为一个为一个java.sql.Timestamp,在Java中,想创建一个java.util.Date,使其值为 '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 3.0.x always converted them to NULL when being read from a ResultSet.

Connector/J 3.1 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 3.1.7, ResultSet.getString() 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://10.149.51.80:3306/test?relaxAutoCommit=true&zeroDateTimeBehavior=convertToNull";

当然,也可以使用另外一个策略:round

分享到:
评论
1 楼 xs.cctv 2013-01-18  
不错   

相关推荐

    nacos-server-2.1.1 适配达梦数据库

    该版本根据nacos源码调整后适配达梦数据库 ,...STU&zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8 db.user.0=nacos db.password.0=123456789 替换数据库连接字符串,以及用户密码

    Demo_vm02.rar

    useSSL=true&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&serverTimezone=Asia&&serverTimezone=GMT%2B8 spring.datasource.username=root spring....

    经过改造可以修改Apache James邮件账户密码的WebMail Claros Intouch2

    2. 数据库连接要加上zeroDateTimeBehavior=convertToNull参数 jdbc:mysql://127.0.0.1/maildb?autoReconnect=true&characterEncoging=utf8&zeroDateTimeBehavior=convertToNull 3. 完整的配置参数参考...

    hibernate多数据库配置

    zeroDateTimeBehavior=convertToNull"/> <bean name="userDataSource" parent="parentDataSource"> <property name="url" value="jdbc:mysql://127.0.0.1:3306/tieba?createDatabaseIfNotExist=true&...

    DmJdbcDriver-1.7.0.jar

    达梦数据库驱动,版本为7.6.0.142 连接示例: jdbc.driver=dm.jdbc.driver....zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8 jdbc.username=SYSDBA jdbc.password=123456789

    springboot+mysql+mybatis+druid实现多数据源配置

    useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=UTC username: root password: 123456 secondary: type: com.alibaba.druid.pool.DruidDataSource ...

    维生药业小项目 SSH简单学习项目

    zeroDateTimeBehavior=convertToNull"> <property name="user" value="root"> <property name="password" value="root"> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation...

    Proxool0.9.1的配置与应用[定义].pdf

    例如,`jdbc:mysql://192.168.104.191:3306/testdb`,其中`useUnicode=true`, `characterEncoding=utf8`, `autoReconnect=true`, `zeroDateTimeBehavior=convertToNull`都是连接参数,用于设置字符编码、自动重连和...

    Mysql JDBC Url参数说明

    zeroDateTimeBehavior=convertToNull ``` 10. **useCompression**: 启用或禁用数据库连接的压缩,可提高网络效率。默认为 `false`。 ```text jdbc:mysql://localhost:3306/mydb?useCompression=true ``` ...

    SQL代码整合

    useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull"; ``` 而在Oracle中,配置字符集则有所不同,因为Oracle默认使用数据库的字符集。要查看当前数据库的字符集,可执行: ```sql ...

    mybatis开发步骤

    zeroDateTimeBehavior=convertToNull" /> <property name="username" value="dev" /> <property name="password" value="" /> <mapper resource="com/mybatis/xinyou/prop/StudentMapper.xml" /> ``` ...

    springboot的yml配置文件通过db2的方式整合mysql的教程

    useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 username: root password: 123456 # 从库数据源 slave: # 从数据源开关/默认关闭 enabled: ...

    凌夕网络QQ业务自助下单平台-其他

    zeroDateTimeBehavior=convertToNull user = 数据库用户名 password = 数据库密码 4、重启Tomcat。 后台路径:http://域名/admin 后台用户名:admin 密码:admin 凌夕网络QQ业务自助下单平台 更新日志: v3.5升级 ...

    JNDI写法tomcat配置

    zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&useLocalSessionState=true&useNewIO=true&cacheServerConfiguration=true" /> ``` - **name**:`MySqlDatasourceForEdu` 表示...

    Wherehows 环境搭建

    charset=utf8&zeroDateTimeBehavior=convertToNull" ``` 最后执行以下命令启动服务: ```sh ./wherehows-1.0-SNAPSHOT/bin/wherehows -Dhttp.port=9019 ``` ##### 5.2 启动Backend-Service 在`backend-service/...

    Spring动态数据源实现读写分离详解

    useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&serverTimezone=PRC&useSSL=false db.master.username=bounter # AES encrypt,Base64 encoded db.master....

    SpringBoot AOP方式实现多数据源切换的方法

    useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull druid.master.username=xxx druid.master.password=123 druid.master.driver-class-name=com.mysql.jdbc.Driver druid.master.max-wait...

    love-sharing-service:爱分享小程序预设原始码

    useSSL=false & useUnicode=true & characterEncoding=utf-8 & zeroDateTimeBehavior=convertToNull & transformedBitIsBoolean=true & serverTimezone=GMT+8 & nullCatalogMeansCurrent=true username: root

    Spring boot 集成 Druid 数据源过程详解

    useUnicode=true&zeroDateTimeBehavior=convertToNull&autoReconnect=true&characterEncoding=utf-8&serverTimezone=GMT%2B8 username: root password: sunday filters: stat,wall,log4j,config max-active: 100...

Global site tag (gtag.js) - Google Analytics