Home > Database > Mysql Tutorial > How to Handle MySQL's 0000-00-00 00:00:00 DATETIME Values in JDBC?

How to Handle MySQL's 0000-00-00 00:00:00 DATETIME Values in JDBC?

Mary-Kate Olsen
Release: 2024-12-25 07:10:17
Original
694 people have browsed it

How to Handle MySQL's 0000-00-00 00:00:00 DATETIME Values in JDBC?

Managing DATETIME Values of 0000-00-00 00:00:00 in JDBC

MySQL's DATETIME data type allows storing "quasi-null" values represented by 0000-00-00 00:00:00. Retrieving such values using JDBC can be problematic, potentially resulting in exceptions or unexpected results.

Alternative Solution without Casting

One approach to handling these values is to modify the JDBC URL, adding the following parameter:

?zeroDateTimeBehavior=convertToNull
Copy after login

This instructs the driver to convert DATETIME values of 0000-00-00 00:00:00 to NULL instead of throwing an exception.

Source: MySQL Manual

The MySQL manual provides additional insights into this issue:

  • All-zero DATETIME values cannot be reliably represented in Java.
  • Connector/J 3.1 throws an exception when encountering such values by default.
  • The zeroDateTimeBehavior property allows customizing this behavior:

    • exception (default): Throws an SQLException.
    • convertToNull: Returns NULL instead of the date.
    • round: Rounds the date to 0001-01-01.

The above is the detailed content of How to Handle MySQL's 0000-00-00 00:00:00 DATETIME Values in JDBC?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template