Java. SQL. date is used in JDBC, but the problem is that only the date data is stored, not the time data. Time data will be lost:
Preparedstatement. setdate (1, new java. SQL. date (date. gettime ()));
Solution: preparedstatement. settimestamp (1, new java. SQL. timestamp (New java. util. Date (). gettime ()));
Use the java. SQL. timestamp class.
Similarly, if you want to obtain complete data, including the date and time
Java. util. Date d = resultset. gettimestamp (1 );
However, this solution is more suitable to avoid some potential timestamp problems.
Java. util. Date d = new java. util. Date (resultset. gettimestamp (1). gettime ());
In short, you can receive Java when storing data in the database. util. date type. Use the gettime () method to obtain the long value representing the date object. Then, use this long value to construct a timestamp object and store it in the database. When getting data from the database, you can first get Timestamp and use its gettime () method to get the long value, and then construct a Java with this long value. util. date object.
The Java. SQL. Date class is not used.