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