码迷,mamicode.com
首页 > 数据库 > 详细

Oracle插入timestamp类型数据

时间:2014-12-07 21:45:56      阅读:283      评论:0      收藏:0      [点我收藏+]

标签:oracle   timestamp   to_timestamp   

表结构:

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


作者:itmyhome

出处:http://blog.csdn.net/itmyhome1990/article/details/41776721





Oracle插入timestamp类型数据

标签:oracle   timestamp   to_timestamp   

原文地址:http://blog.csdn.net/itmyhome1990/article/details/41776721

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!