MySql与Java的时间类型
MySql与Java的时间类型 MySql的时间类型有Java中与之对应的时间类型 datejava.sql.Date Datetimejava.sql.Timestamp Timestampjava.sql.Timestamp Timejava.sql.Time Yearjava.sql.Date 对其进行分析 参考MySql的reference manual Date: A date. The support
MySql与Java的时间类型
MySql的时间类型有 Java中与之对应的时间类型
date java.sql.Date
Datetime java.sql.Timestamp
Timestamp java.sql.Timestamp
Time java.sql.Time
Year java.sql.Date
对其进行分析
参考MySql 的reference manual
Date:
A date. The supported range is '1000-01-01' to '9999-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.
只记录日期信息,表示范围为1000-01-01 至 9999-12-31。
MySql 按照YYYY-MM-DD 的方式进行该类字段的显示。添加该类字段数据,即可以使用字符串类型,也可以使用数字类型
由于Date类型的字段只记录日期信息,所以如果添加的数据中包含了时间信息,该时间信息将会自动被截断。
如果要保存时间信息,可以考虑使用DateTime类型。
经过测试,发现如下2种方式可以对Date类型字段进行填充:
按字符串:
insert into time_table(CreateDate) values(‘2007-04-09’)
按数字:
insert into time_table(CreateDate) values(20070409)
获取可以用java.sql.Date类型获取
代码为:
Date dtDate =rsBuffer.getDate("CreateDate");
测试代码如下:(其中,IDBFace 是自己基于JDBC封装的一个简单类, 接受Sql对数据库进行操作)
public void testDate()throws SQLException
{
IDBFace DBFace =DBFactory.createMySqlFace();
DBFace.connect();
//清空表
String strDelete ="delete from time_table";
DBFace.update(strDelete);
//添加
String strInsert ="insert into time_table(CreateDate) values(20070409)";
DBFace.update(strInsert);
//获取
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();
}
执行结果: 2007-04-09
DateTime
A date and time combination. The supported range is '1000-01-01 00:00:00'
to '9999-12-31 23:59:59'
. MySQL displays DATETIME
values in 'YYYY-MM-DD
HH:MM:SS'
format, but allows you to assign values to DATETIME
columns using either strings or numbers.
DateTime 与Date最主要的区别在于:DateTime 可以记录日期和时间信息。而Date只记录日期信息。表示范围为: 1000-01-01 00:00:00 至 9999-12-31 23:59:59 MySql的按照YYYY-MM-DD HH:MM:SS对数据进行格式化,允许以字符串和数字的方式提交。
例如以数字的方式进行提交:
insert into time_table(CreateDate) values(20070409132013)
获取该类型的数据可以使用:java.sql.Timestamp类型
代码如下:
public void testDateTime() throws SQLException
{
IDBFace DBFace =DBFactory.createMySqlFace();
DBFace.connect();
//清空表
String strDelete ="delete from time_table";
DBFace.update(strDelete);
//添加
String strInsert ="insert into time_table(CreateDateTime) values(20070409132013)";
DBFace.update(strInsert);
//获取
String strSelect ="select * from time_table";
ResultSet rsBuffer =DBFace.select(strSelect);
while(rsBuffer.next())
{
Timestamp tsBuffer =rsBuffer.getTimestamp("CreateDateTime");
System.out.println(tsBuffer.toString());
}
DBFace.close();
}
执行结果: 2007-04-09 13:20:13.0
TimeStamp
A timestamp. The range is '1970-01-01 00:00:00'
to partway through the year 2037
. A TIMESTAMP
column is useful for recording the date and time of an INSERT
or UPDATE
operation.
The firstTIMESTAMP
column in a table is automatically set to the date and time of the most recent operation if you don't assign it a value yourself. You can also set any TIMESTAMP
column to the current date and time by assigning it
a NULL
value.
与DateTime类型非常相似
范围为1970-01-01 –2037年,精度为1秒/
如果在Sql中未对Timestamp类型的列赋值,该列将被构造成当前时间。
提交NULL值也会使该列以当前时间录入。
如果时间提交错误,该列将被填入0.
Timestamp比DateTime 类型所需的存储空间更小,只需要4个字节,而DateTime需要8个字节。
但是有一点需要特别注意。Timestamp只能表示时间范围为1970 -2037.
使用Timestamp一定要确保提交的时间数据一定不会超过这个范围。
代码与DateTime类是,而且我不喜欢用,所以略掉了。
Time:
A time. The range is <span>'-838:59:59'</span>
to <span>'838:59:59'</span>
.
MySQL displays <span>TIME</span>
values in <span>'HH:MM:SS'</span>
format, but allows
you to assign values to <span>TIME</span>
columns using either strings or numbers.
Time只记录时间信息,不包含日期信息。
范围为-838:59:59 到 838:59:59, MySql 以HH:MM:SS格式化该数据,允许输入为字符串或者数字。
代码:
public void testTime() throws SQLException
{
IDBFace DBFace =DBFactory.createMySqlFace();
DBFace.connect();
//清空表
String strDelete ="delete from time_table";
DBFace.update(strDelete);
//添加
String strInsert ="insert into time_table(CreateTime) values(131211)";
DBFace.update(strInsert);
//获取
String strSelect ="select * from time_table";
ResultSet rsBuffer =DBFace.select(strSelect);
while(rsBuffer.next())
{
Time tmBuffer =rsBuffer.getTime("CreateTime");
System.out.println(tmBuffer.toString());
}
DBFace.close();
}
执行结果: 13:12:11
Year
A year in two-digit or four-digit format. The default is four-digit format. In four-digit format, the allowable values are <span>1901</span>
to <span>2155</span>
,
and <span>0000</span>
. In two-digit format, the allowable values are <span>70</span>
to <span>69</span>
,
representing years from 1970 to 2069. MySQL displays <span>YEAR</span>
values in <span>YYYY</span>
format,
but allows you to assign values to <span>YEAR</span>
columns using either strings or numbers. The <span>YEAR</span>
type
is unavailable prior to MySQL 3.22.
Year可以有2种表示方式,4位的和2位的。
默认情况是4位。其范围为1901-2155
2位的表述法只记录后2位 。其范围为1970-2069
允许以字符串或者数字的方式插入。
代码:
public void testYear() throws SQLException
{
IDBFace DBFace =DBFactory.createMySqlFace();
DBFace.connect();
//清空表
String strDelete ="delete from time_table";
DBFace.update(strDelete);
//添加
String strInsert ="insert into time_table(CreateYear) values(2007)";
DBFace.update(strInsert);
//获取
String strSelect ="select * from time_table";
ResultSet rsBuffer =DBFace.select(strSelect);
while(rsBuffer.next())
{
Date dtBuffer =rsBuffer.getDate("CreateYear");
System.out.println(dtBuffer.getYear()+1900);
}
DBFace.close();
}
执行结果:2007
需要说明的是:
Date.getYear()方法返回至1900年起经过了多少年。所以为了显示正确的时间,必须加上1900.
该方法已经被废弃。
另外。
有一种方法是:不使用上述任何一种类型来记录时间。
而是以char(或vchar)的方式来记录时间。
这样做在插入数据和显示记录的时候固然不用进行任何转换而比较方便。
但是要承担2个重要的缺陷。
(1) 要单独开发方法验证时间数据的合法性。例如ajidjieoa字符串不是一个时间信息,但仍然可以正常插入。
(2) 如果系统需将时间范围做为条件进行记录检索。这也会是一个*烦。用字符串记录时间将无法使用MySql为时间提供的API.对时间范围检索的代码可能与数据库剥离。这样对性能必然造成影响。例如,要从100万条数据中查询时间范围为1992-3-12 –1992-3-13日的区区100条数据,你可能不得不将100万条数据都查出来,再开发新的方法进行过滤。
另外,MySql到4.1时间精度貌若只到秒。
要记录更细的时间粒度。可以考虑构造DateTime.
记录DateTime.trick().
这只是一个想法,有没有额外的问题尚未证明。/

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

在MySQL中,添加字段使用ALTERTABLEtable_nameADDCOLUMNnew_columnVARCHAR(255)AFTERexisting_column,刪除字段使用ALTERTABLEtable_nameDROPCOLUMNcolumn_to_drop。添加字段時,需指定位置以優化查詢性能和數據結構;刪除字段前需確認操作不可逆;使用在線DDL、備份數據、測試環境和低負載時間段修改表結構是性能優化和最佳實踐。

要安全、徹底地卸載MySQL並清理所有殘留文件,需遵循以下步驟:1.停止MySQL服務;2.卸載MySQL軟件包;3.清理配置文件和數據目錄;4.驗證卸載是否徹底。

MySQL批量插入数据的高效方法包括:1.使用INSERTINTO...VALUES语法,2.利用LOADDATAINFILE命令,3.使用事务处理,4.调整批量大小,5.禁用索引,6.使用INSERTIGNORE或INSERT...ONDUPLICATEKEYUPDATE,这些方法能显著提升数据库操作效率。

MySQL函數可用於數據處理和計算。 1.基本用法包括字符串處理、日期計算和數學運算。 2.高級用法涉及結合多個函數實現複雜操作。 3.性能優化需避免在WHERE子句中使用函數,並使用GROUPBY和臨時表。

在macOS上安裝MySQL可以通過以下步驟實現:1.安裝Homebrew,使用命令/bin/bash-c"$(curl-fsSLhttps://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"。 2.更新Homebrew,使用brewupdate。 3.安裝MySQL,使用brewinstallmysql。 4.啟動MySQL服務,使用brewservicesstartmysql。安裝後,可通過mysql-u

使用EXPLAIN命令可以分析MySQL查詢的執行計劃。 1.EXPLAIN命令顯示查詢的執行計劃,幫助找出性能瓶頸。 2.執行計劃包括id、select_type、table、type、possible_keys、key、key_len、ref、rows和Extra等字段。 3.根據執行計劃,可以通過添加索引、避免全表掃描、優化JOIN操作和使用覆蓋索引來優化查詢。

MySQL被廣泛應用於各種項目中的原因包括:1.高性能與可擴展性,支持多種存儲引擎;2.易於使用和維護,配置簡單且工具豐富;3.豐富的生態系統,吸引大量社區和第三方工具支持;4.跨平台支持,適用於多種操作系統。

在MySQL中配置字符集和排序規則的方法包括:1.設置服務器級別的字符集和排序規則:SETNAMES'utf8';SETCHARACTERSETutf8;SETCOLLATION_CONNECTION='utf8_general_ci';2.創建使用特定字符集和排序規則的數據庫:CREATEDATABASEexample_dbCHARACTERSETutf8COLLATEutf8_general_ci;3.創建表時指定字符集和排序規則:CREATETABLEexample_table(idINT
