◎JAVA对ORACLE中日期型字段的操作

◎JAVA对ORACLE中日期型字段的操作

方法一:
使用java.sql.Date实现在yyyy-mm-dd格式的日期。

java.sql.Date不支持时间格式。切不要使用new java.sql.Date(int year, int month, int date),因为这样还需要处理时间差问题。

PreparedStatement pstmt = conn.prepareStatement("insert into table1(tdate) values (?)");

java.sql.Date tdate= java.sql.Date.valueOf("2007-04-09");
pstmt.setDate(1,tdate );
pstmt.execute();

方法二:
使用java.sql.Timestamp,同上不要使用new java.sql.Timestamp(...)

PreparedStatement pstmt = conn.prepareStatement("insert into table1 (tdate) values (?)");

java.sql.Timestamp tdate= java.sql.Timestamp.valueOf("2007-04-09 10:50:00");
pstmt.setTimestamp(1,tdate );
pstmt.execute();

方法三:
使用ORACLE的内置函数to_date

PreparedStatement pstmt  =  conn.prepareStatement( " insert into table1 (tdate) values (to_date(?, 'yyyy-mm-dd hh24:mi:ss') " );

String buydate
= "2007-04-09 10:50:00 " ;
pstmt.setString(1
,buydate );
pstmt.execute();

你可能感兴趣的:(◎JAVA对ORACLE中日期型字段的操作)