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.
- date java.sql.Date
- Datetime java.sql.Timestamp
- Timestamp java.sql.Timestamp
- Time java.sql.Time
- 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:
- insert into time_table(CreateDate) values(‘2007-04-09’)
By number:
- insert into time_table(CreateDate) values(20070409)
You can use java. SQL. Date to obtain the data.
Code:
- 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)
- public void testDate()throws SQLException
- {
- IDBFace DBFace =DBFactory.createMySQLFace();
- DBFace.connect();
Clear table
- String strDelete ="delete from time_table";
- DBFace.update(strDelete);
Add
- String strInsert ="insert into time_table(CreateDate) values(20070409)";
- DBFace.update(strInsert);
Obtain
- String strSelect ="select * from time_table";
- ResultSet rsBuffer =DBFace.select(strSelect);
- while(rsBuffer.next())
- {
- Date dtDate =rsBuffer.getDate("CreateDate");
- System.out.println(dtDate.toString());
- }
- DBFace.close();
- }
-
Execution result:. The above content is an introduction to the time type of MySQL database and Java. I hope you will get some benefits.