Date, Time, And Timestamp Values That Can Cause Problems In Jdbc And Sqlj Applications - IBM DB2 Manual

Table of Contents

Advertisement

Date, time, and timestamp values that can cause problems in
|
JDBC and SQLJ applications
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
Table 29. Examples of updating DATE, TIME, or TIMESTAMP SQL values with Java date, time, or timestamp values
|
that do not represent real dates or times
|
Target type in
|
| |
String input value
database
| | |
2008-13-35
DATE
200
Application Programming Guide and Reference for Java
You might receive unexpected results in JDBC and SQLJ applications if you use
date, time, and timestamp values that do not correspond to real dates and times. In
particular, using the hour '24' to represent midnight can cause problems.
The IBM Data Server Driver for JDBC and SQLJ uses Java data types for its
internal processing of input and output parameters and ResultSet content in JDBC
and SQLJ applications. The Java data type that is used by the driver is based on
the best match for the corresponding SQL type when the target SQL type is known
to the driver.
For values that are assigned to or retrieved from DATE, TIME, or TIMESTAMP
SQL types, the IBM Data Server Driver for JDBC and SQLJ uses java.sql.Date for
DATE SQL types, java.sql.Time for TIME SQL types, and java.sql.Timestamp for
TIMESTAMP SQL types.
When you assign a string value to a DATE, TIME, or TIMESTAMP target, the IBM
Data Server Driver for JDBC and SQLJ uses Java facilities to convert the string
value to a java.sql.Date, java.sql.Time, or java.sql.Timestamp value. If a string
representation of a date, time, or timestamp value does not correspond to a real
date or time, Java adjusts the value to a real date or time value. In particular, Java
adjusts an hour value of '24' to '00' of the next day. This adjustment can result in
an exception for a timestamp value of '9999-12-31 24:00:00.0', because the adjusted
year value becomes '10000'.
Important: To avoid unexpected results when you assign or retrieve date, time, or
timestamp values in JDBC or SQLJ applications, ensure that the values are real
date, time, or timestamp values. In addition, do not use '24' as the hour
component of a time or timestamp value.
If a value that does not correspond to a real date or time, such as a value with an
hour component of '24', is stored in a TIME or TIMESTAMP column, you can
avoid adjustment during retrieval by executing the SQL CHAR function against
that column in the SELECT statement that defines a ResultSet. Executing the
CHAR function converts the date or time value to a character string value on the
database side. However, if you use the getTime or getTimestamp method to retrieve
that value from the ResultSet, the IBM Data Server Driver for JDBC and SQLJ
converts the value to a java.sql.Time or java.sql.Timestamp type, and Java adjusts
the value. To avoid date adjustment, execute the CHAR function against the
column value, and retrieve the value from the ResultSet with the getString
method.
Examples
The following examples show the results of updating DATE, TIME, or
TIMESTAMP columns in JDBC or SQLJ applications, when the application data
does not represent real dates or times.
Value sent to table column, or exception
2009-02-04

Hide quick links:

Advertisement

Table of Contents
loading

Table of Contents