handling DATETIME values 0000-00-00 00:00:00 in JDBC

前端 未结 10 1678
一向
一向 2020-11-27 11:01

I get an exception (see below) if I try to do

resultset.getString(\"add_date\");

for a JDBC connection to a MySQL database containing a DA

10条回答
  •  独厮守ぢ
    2020-11-27 11:16

    My point is that I just want the raw DATETIME string, so I can parse it myself as is.

    That makes me think that your "workaround" is not a workaround, but in fact the only way to get the value from the database into your code:

    SELECT CAST(add_date AS CHAR) as add_date
    

    By the way, some more notes from the MySQL documentation:

    MySQL Constraints on Invalid Data:

    Before MySQL 5.0.2, MySQL is forgiving of illegal or improper data values and coerces them to legal values for data entry. In MySQL 5.0.2 and up, that remains the default behavior, but you can change the server SQL mode to select more traditional treatment of bad values such that the server rejects them and aborts the statement in which they occur.

    [..]

    If you try to store NULL into a column that doesn't take NULL values, an error occurs for single-row INSERT statements. For multiple-row INSERT statements or for INSERT INTO ... SELECT statements, MySQL Server stores the implicit default value for the column data type.

    MySQL 5.x Date and Time Types:

    MySQL also allows you to store '0000-00-00' as a “dummy date” (if you are not using the NO_ZERO_DATE SQL mode). This is in some cases more convenient (and uses less data and index space) than using NULL values.

    [..]

    By default, when MySQL encounters a value for a date or time type that is out of range or otherwise illegal for the type (as described at the beginning of this section), it converts the value to the “zero” value for that type.

提交回复
热议问题