ySql 및 Java 시간 유형 확인
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-09DateTimeA 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의 YYY-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.0TimeStampa 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 first TIMESTAMP 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 C2037년이며 정밀도는 1초/Sql에서 Timestamp 유형의 열에 값을 부여하지 않으면 현재 시간으로 구성됩니다.NULL 값을 커밋하면 열이 현재 시간으로 기록됩니다.시간 제출 오류가 발생하면 이 열은 0으로 채워집니다.Timestamp는 DateTime 유형보다 저장 공간이 작아 4바이트만 필요하고 DateTime은 8바이트가 필요합니다.하지만 한 가지 각별한 주의가 필요하다.Timestamp은 시간 범위가 1970-2037임을 나타낼 수 있습니다.Timestamp을 사용하려면 제출 시간 데이터가 이 범위를 넘지 않도록 해야 합니다.코드와 DateTime 클래스는 그렇고 사용하기 싫어서 생략했습니다.Time:A time. The range is '-838:59:59' to '838:59:59'. MySQL displays TIME values in 'HH:MM:SS' format, but allows you to assign values to TIME 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:11YearA year in two-digit or four-digit format.The default is four-digit format. In four-digit format, the allowable values are 1901 to 2155, and 0000. In two-digit format, the allowable values are 70 to 69, representing years from 1970 to 2069. MySQL displays YEAR values in YYYY format, but allows you to assign values to YEAR columns using either strings or numbers. The YEAR type is unavailable prior to MySQL 3.22. Year는 두 가지 표현 방식이 있는데, 네 자리와 두 자리다.기본값은 4자리입니다.그 범위는 1901-21552위의 표현법으로 뒷부분만 기록한다.그 범위는 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)의 방식으로 시간을 기록합니다.이렇게 하면 데이터를 삽입하고 기록을 표시할 때 어떤 전환도 하지 않아도 비교적 편리하다.하지만 두 가지 중요한 결함을 짊어져야 한다.(1) 단독 개발 방법으로 시간 데이터의 합법성을 검증해야 한다.예를 들어 ajidjieoa 문자열은 시간 정보가 아니지만 정상적으로 삽입할 수 있습니다.(2) 기록 검색을 위해 시간 범위를 기준으로 해야 하는 경우이것도 큰 골칫거리가 될 것이다.문자열로 시간을 기록하면 MySql에서 제공하는 API를 사용할 수 없습니다.시간 범위에서 검색한 코드는 데이터베이스와 박리될 수 있습니다.이렇게 하면 성능에 필연적으로 영향을 끼친다.예를 들어 100만 개의 데이터에서 시간 범위가 1992-3-12 C1992-3-13일인 100개의 데이터를 조회하려면 100만 개의 데이터를 모두 찾아내고 새로운 방법을 개발하여 필터를 해야 할 수도 있다.또한 MySql에서 4.1까지의 시간 정밀도는 초까지입니다.더 세밀한 시간 입도를 기록해야 한다.DateTime 구성을 고려할 수 있습니다.DateTime을 기록합니다.trick().이것은 단지 하나의 생각일 뿐, 별도의 문제가 있는지는 아직 증명되지 않았다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
자바를 잡아버려 (1)나의 생각을 적고 복습을 해버릴 것 이다 책을 펼치자 마자 나오는 설명인데 그 안의 내용을 실행하게 된다 라고 설명을 해준다 아래 소스코드와 실행 결과로 위에 설명을 보충해준다 사칙연산과 나머지를 계산하는 것 비교연...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.