Oracle插入timestamp類型資料

來源:互聯網
上載者:User

Oracle插入timestamp類型資料

Oracle表結構:

create table TEST(
  ID      INTEGER,
  BIRTHDAY TIMESTAMP
);

使用JDBC將日期插入到TIMESTAMP類型欄位

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Test {
 
 static {
  try {
   Class.forName("oracle.jdbc.driver.OracleDriver");
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  }
 }

 /**
  * 獲得Connection
  *
  * @return
  */
 public static Connection getConnection() {
  Connection conn = null;
  try {
   conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521/orcl", "root", "root");
  } catch (SQLException e) {
   e.printStackTrace();
  }
  return conn;
 }

 public static void main(String[] args) throws SQLException {
  // TODO Auto-generated method stub
  PreparedStatement pst = null;
  Connection conn = getConnection();
  SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  String myDate = df.format(new Date()); //目前時間
  String sql = "insert into test(id,birthday) values('1',to_timestamp('" + myDate + "','yyyy-mm-dd hh24:mi:ss'))";
  pst = conn.prepareStatement(sql);
 
  pst.executeUpdate();
  // 省略IO流close
 }

}

使用to_timestamp將字元型轉成timestamp

在CentOS 6.4下安裝Oracle 11gR2(x64)

Oracle 11gR2 在VMWare虛擬機器中安裝步驟

Debian 下 安裝 Oracle 11g XE R2

Oracle Linux 6.5安裝Oracle 11.2.0.4 x64

相關文章

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.