Detailed analysis of MySQL database and Java time types

Source: Internet
Author: User

The following articles mainly introduce the time types of MySQL Databases and Java databases. The following articles mainly describe the time types corresponding to the time types in MySQL Databases and Java databases, the following is a detailed description of the article. I hope you will gain some benefits after browsing it.

The time types of the MySQL database include the corresponding time types in Java.

 
 
  1. date java.sql.Date  
  2. Datetime java.sql.Timestamp  
  3. Timestamp java.sql.Timestamp  
  4. Time java.sql.Time  
  5. Year java.sql.Date  

Analyze it

Refer to MySQL reference manual

Date:

A date. the supported range is '2017-01-01 'to '2017-12-31 '. mySQL displays DATE values in 'yyyy-MM-DD 'format, but allows you to assign values to DATE columns using either strings or numbers.

Only date information is recorded, indicating that the range is 1000-01-01 to 9999-12-31.

The MySQL database displays this type of fields in YYYY-MM-DD. You can use the string type or numeric type to add the field data.

Because a Date field only records the Date information, if the added data contains the time information, the time information will be automatically truncated.

If you want to save the time information, you can use the DateTime type.

After testing, we found that the following two methods can be used to fill the Date field:

Press string:

 
 
  1. insert into time_table(CreateDate) values(‘2007-04-09’) 

By number:
 

 
 
  1. insert into time_table(CreateDate) values(20070409) 

You can use java. SQL. Date to obtain the data.

Code:

 
 
  1. Date dtDate =rsBuffer.getDate("CreateDate"); 

The time type test code for MySQL database and Java is as follows: (IDBFace is a simple class encapsulated by itself based on JDBC, and the database is operated by SQL)

 
 
  1. public void testDate()throws SQLException  
  2. {  
  3. IDBFace DBFace =DBFactory.createMySQLFace();  
  4. DBFace.connect();  

Clear table

 
 
  1. String strDelete ="delete from time_table";  
  2. DBFace.update(strDelete);  

Add

 
 
  1. String strInsert ="insert into time_table(CreateDate) values(20070409)";  
  2. DBFace.update(strInsert);  

Obtain

 
 
  1. String strSelect ="select * from time_table";  
  2. ResultSet rsBuffer =DBFace.select(strSelect);  
  3. while(rsBuffer.next())  
  4. {  
  5. Date dtDate =rsBuffer.getDate("CreateDate");  
  6. System.out.println(dtDate.toString());  
  7. }  
  8. DBFace.close();  
  9. }  
  10.  

Execution result:. The above content is an introduction to the time type of MySQL database and Java. I hope you will get some benefits.

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.