The date type processing of JDBC solves the loss of time data between Java. util. Date and Java. SQL. Date.

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.