java – ResultSet. getTimestamp(“date”)vs ResultSet. getTimestamp(“date”,Calendar.getInstance(tz))

java. util. Date,java. util. Timetamp seems to have caused great confusion to many people There are so many problems in stackoverflow. Unfortunately, my problem is a little distorted

There are 2 JDBC APIs How should they behave? Is there consistency in the RDBMS?

ResultSet.getTimestamp("dateColumn") 
ResultSet.getTimestamp("dateColumn",Calendar.getInstance(tz))

If someone has knowledge in Sybase, can you share your experience?

Solution

First, you will Java Util and Java SQL confusion When using Preparedstatement #setdate() and resultset #getdate(), you need Java sql. Date. Similarly, when using Preparedstatement #settimestamp() and resultset #gettimestamp(), you need Java sql. Timestamp.

Second, understand java sql. Date only represents date (year, month, day) and less or more This will map to the SQL date field type java. sql. Timestamp represents the timestamp (year, day, hour, minute, second, millisecond), which is similar to Java util. Date and Java util. Calendar is exactly the same This will map to the SQL timestamp or datetime field type

For time zones, you need it when the database does not store time zone information (therefore, all timestamps are stored in UTC (GMT)) Then, you can pass the calendar containing the current time zone information so that the jdbc driver can adjust the UTC timestamp to match the time zone If it is, for example, GMT 1, the jdbc driver will add an hour to the timestamp before returning

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>