JDBC – 日期時間欄位

※問題

如何 insert 一個日期時間欄位。

※解決方法

  • 存入目前日期時間

Calendar cal = Calendar.getInstance();
stam.setTimestamp(1, new java.sql.Timestamp(cal.getTimeInMillis()));

  • 存入設定的日期

Calendar cal = Calendar.getInstance();
cal.set(2008, 10 – 1, 1);//2008年10月1日
stam.setDate(1, new java.sql.Date(cal.getTimeInMillis()));

另外要注意的是,MySQL的日期時間欄位要選擇

  • date:只顯示日期
  • datetime:顯示日期時間
  • timestamp:顯示日期時間(有選項可勾選自動取得目前時間)

0 回應 to “JDBC – 日期時間欄位”



  1. 發表迴響

發表迴響

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 變更 )

Twitter picture

You are commenting using your Twitter account. Log Out / 變更 )

Facebook照片

You are commenting using your Facebook account. Log Out / 變更 )

連結到 %s





Follow

Get every new post delivered to your Inbox.