java insert time to ORACLE

public class Test{
        public static void main (String args []){              
                java.util.Date a = new java.util.Date();
                System.out.println(a);
                java.sql.Date b = new java.sql.Date(a.getTime());
                System.out.println(b);
                java.sql.Time c = new java.sql.Time(a.getTime());
                System.out.println(c);
                java.sql.Timestamp d=new java.sql.Timestamp(a.getTime());
                System.out.println(d);
        }
}

===The following is the output ===

Mon Apr 03 18:00:34 CST 2006
2006-04-03
18:00:34
2006-04-03 18:00:34.388

=====Description=====

1. Oracle's default system time is the sysdate function, and the stored data is in the form of 25-3-2005 10:55:33.

2. The time object in java is java.util.Date.

3. The corresponding time objects in oracle are java.sql.Date, java.sql.Time, java.sql.Timestamp, and they are all subclasses of java.util.Date.

4. Two conversion functions are most closely related to date operations in oracle: to_date(), to_char(). to_date() is generally used for functions used when writing dates to the database. to_char() is generally used for functions used when reading dates from the database.

DATE、TIME 和 TIMESTAMP:
SQL defines three time-related data types: DATE consists of day, month, and year. TIME consists of hours, minutes, and seconds. TIMESTAMP combines DATE and TIME and adds a nanosecond field.
The standard Java class java.util.Date provides date and time information. But since this class contains DATE and TIME information without the nanoseconds required by TIMESTAMP, it does not exactly match the three SQL types above.
So we define three subclasses of java.util.Date. They are:
1. java.sql.Date for SQL DATE information
2. java.sql.Time for SQL TIME information
3. java.sql.Timestamp for SQL TIMESTAMP information
For java.sql.Time, the hour, minute, second, and millisecond fields of the java.util.Time base class are set to zero. For java.sql.Date, the year, month, and day fields of the java.util.Date base class are set to January 1, 1970, respectively. This is the "zero" date in the new era of Java. 
Dates in java.sql.date can be compared with fields containing dates in standard SQL statements. The java.sql.Timestamp class extends java.util.Date by adding a nanosecond field.

If the time type of the oracle database field is date type, it is best to use java.sql.Date type to operate when inserting data. The database may only have the year, month and day.
If the time type is used in the oracle time field, use java. sql.Time type to operate, may only have hours, minutes and seconds. Similarly, if it is a TIMESTAMP time type in the oracle database, use java.sql.Timestamp, which will fully represent the time, year, month, day, hour, minute, and second.


Two conversion functions in oracle:

1. The to_date() function converts the character type into a date type in a certain format:

Specific usage: to_date(''2004-11-27'',''yyyy-mm-dd''), the former is a string, and the latter is a converted date format. Note that the two should be in one correspondence. For example; to_date(''2004-11-27 13:34:43'', ''yyyy-mm-dd hh24:mi:ss'') will get the specific time.

2. to_char(): Convert the date to a character type in a certain format:

Specific usage: to_char(sysdate,''yyyy-mm-dd hh24:mi:ss'')

to_date() with 24-hour notation and display of mm minutes:

When using Oracle's to_date function for date conversion, many Java programmers may directly use the format of "yyyy-MM-dd HH:mm:ss" as the format for conversion, but it will cause an error in Oracle: "ORA 01810 format code appears twice".

Such as: select to_date('2005-01-01 13:14:20','yyyy-MM-dd HH24:mm:ss') from dual;
the reason is that SQL is case insensitive, MM and mm are considered the same format code, so Oracle's SQL uses mi instead of minutes. Oracle's default system time is the sysdate function, the stored data is in the form of 2005-3-2510:55:33, and the object that takes the time in java is java.util.Date.

select to_date('2005-01-01 13:14:20','yyyy-MM-dd HH24:mi:ss') from dual

In java's operation on oracle, an example of operating on a date field:

Table book has name varchar2(20)//book name, buydate Date //two fields of purchase date. Database connection Connection conn has been created;

 

 

Method 1. Use java.sql.Date to implement a relatively simple yyyy-mm-dd format date. java.sql.Date does not support time formats. Remember not to use new java.sql.Date(int year, int month, int date), because you also have to deal with the time difference. 

PreparedStatement pstmt = conn.prepareStatement("insert into book (name,buydate) values ​​(?,?)" ); java.sql.Date buydate=java.sql.Date.valueOf("2005-06-08"); pstmt.setString( 1, "Java programming ideas" ); pstmt.setDate(2,buydate ); pstmt.execute();


Method 2. Use java.sql.Timestamp, ibid. do not use new Timestamp(....)

PreparedStatement pstmt = conn.prepareStatement("insert into book (name,buydate) values (?,?)");
java.sql.Timestamp buydate=java.sql.Timestamp.valueOf("2004-06-08 05:33:99");
pstmt.setString( 1, "Java programming ideas" );
pstmt.setTimestamp ( 2 , buydate);
pstmt.execute();
Method 3. Use oracle's to_date built-in function

PreparedStatement pstmt = conn.prepareStatement("insert into book (name,buydate) values (?,to_date(?, 'yyyy-mm-dd hh24:mi:ss')");
String buydate="2004-06-08 05:33:99";
pstmt.setString( 1, "Java programming ideas" );
pstmt.setString ( 2 , buydate);
pstmt.execute();
Attachment: Explanation of the meaning of oracle date format parameters
d: day of the week
day: The name of the day, padded with spaces to 9 characters
dd: day of the month
ddd: day of the year
dy: short name of day
iw: ISO standard week of the year
iyyy: ISO standard four-digit year
yyyy: four-digit year
yyy,yy,y: the last three digits of the year, two digits, one digit
hh: hours, in 12 hours
hh24: hours, in 24 hours
mi: minutes
ss: seconds
mm:month
mon: shorthand for month
month: full name of the month
w: the week of the month
ww: week of the year
sql111= select * from  logincount where logtime = to_date('2009-11-30','yyyy-MM-dd')
st = con.prepareStatement("select * from  logincount where logtime = to_date('"+ new java.sql.Date(date.getTime())+"','yyyy-MM-dd')");

Source address: http://www.tbdazhe.com/archives/462

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325128610&siteId=291194637