向Oracle中插入date資料類型

來源:互聯網
上載者:User

1.利用Oracle中的方法to_date();

String   DateStr   =   "1985-07-11   10:30:00.0 ";

pstmt   =   conn.prepareStatement( "INSERT   INTO   t(d)   VALUES(to_date(?, 'YYYY-MM-DD   HH24:MI:SS ') ");


pstmt.setString(1,   DateStr  )

2.java.sql.Date 不同於java.util.Date,前者是子類。pstmt.setDate方法參數類型是sql.Date。

因此需要轉換

day = new SimpleDateFormat().parse(DateStr,"yyyy-MM-DD  HH:mm:ss");

sql.Date sday =  new Date(day.getTimes());//

pstmt.setDate(sday);

但是只保留了日期,而沒有小時,如果想儲存到24小時精確時間,用下一種。

3.

   java.util.Date Dates = new java.util.Date();
        java.sql.Timestamp time = new java.sql.Timestamp(Dates.getTime());
        ......
       psmt.setTimestamp(3, time);

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.