ySql 및 Java 시간 유형 확인

MySql의 시간 형식은 Java에서 대응하는 시간 형식date java가 있습니다.sql.DateDatetime                                    java.sql.TimestampTimestamp                                  java.sql.TimestampTime                                          java.sql.TimeYear                                           java.sql.Date는 이를 분석하여 MySql의 reference manualDate: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입니다.이 필드는 YYYY-MM-DD 방식으로 MySql에 표시됩니다.클래스 필드 데이터를 추가합니다. 문자열 형식을 사용할 수도 있고, 숫자 형식을 사용할 수도 있습니다. Date 형식의 필드는 날짜 정보만 기록하기 때문에 추가된 데이터에 시간 정보가 포함되면 자동으로 차단됩니다.시간 정보를 저장하려면 DateTime 유형을 사용하는 것이 좋습니다.테스트를 통해 다음과 같은 두 가지 방법으로 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-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().이것은 단지 하나의 생각일 뿐, 별도의 문제가 있는지는 아직 증명되지 않았다.

좋은 웹페이지 즐겨찾기