通过JDBC往Oracle中插入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来达到插入timestamp数据的目的。但是当前的insert语句只使用于oracle,当环境变更为mysql时则不行(没有to_timestamp函数)。
为此我们做出如下修改:
package com.ssh.model; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Timestamp; 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(); String sql = "insert into test(id,birthday) values('1'," + new Timestamp(System.currentTimeMillis()) + ")"; pst = conn.prepareStatement(sql); pst.executeUpdate(); // 省略IO流close } }
这样做到了兼容。
相关推荐
oracle中TIMESTAMP与DATE比较
5、DATE数据类型,使用7个字节固定长度,每个字节分别存储世纪,年,月,日 ,时,分,秒,ORACLE中SYSDATE函数的功能是返回当前的日期和时间 6、TIMESTAMP数据类型,和DATE相似,但是这个类型的秒精确到小数点后6...
oracle --timestamporacle --timestamporacle --timestamporacle --timestamporacle --timestamporacle --timestamporacle --timestamporacle --timestamp
对oracle date,timestamp,interval数据类型的详细讲解。
oracle timestamp详解 将常用的转换方法及使用注意事项都罗列出来了
这一天终于到来,在11g中,引用oracle.jdbc.driver不再通过编译。 3.j2se 1.2,1.3,1.4不再支持。11R1不再包括这些版本的jar和zip,如果仍然使用这些版本,可以继续使用10gR2的jdbc。 4.11gR1 Thin driver...
oracle中日期类型与unix 时间戳的转换, Unix时间戳记是从'1970-01-01 00:00:00'GMT开始的秒数,表现为整数型。 Oracle中的时间是Date型,以下函数提供了两种时间转换的Oracle函数 --unix时间戳与date时间互转 ...
Oracle date 和 timestamp 区别详解 1.DATE数据类型 这个数据类型我们实在是太熟悉了,当我们需要表示日期和时间的话都会想到date类型。它可以存储月,年,日,世纪,时,分和秒。它典型地用来表示什么时候事情...
我们都知道date和timestamp都是对日期和时间的表示,只是两种类型的精确度不同,前者精确到秒,后者精确到小数秒(fractional_seconds...这篇文章主要介绍了Oralce中TIMESTAMP的几种类型。有需要的朋友们可以参考借鉴。
NULL 博文链接:https://bchen.iteye.com/blog/1632845
这一天终于到来,在11g中,引用oracle.jdbc.driver不再通过编译。 3.j2se 1.2,1.3,1.4不再支持。11R1不再包括这些版本的jar和zip,如果仍然使用这些版本,可以继续使用10gR2的jdbc。 4.11gR1 Thin driver支持...
只有日期 可以用Timestamp+SimpleDateFormat解决
在Oracle 9i中,按照SQL 99标准,增加了时间间隔型数据INTERVAL YEAR TO MONTH 和 INTERVAL DAY TO SECOND,它们和其他几种数据类型一起使得对时间的处理更加准确。TIMESTAMP、TIMESTAMP WITH TIME ZONE和TIMESTAMP ...
NULL 博文链接:https://zhengkaifl.iteye.com/blog/734139
1、新建链接服务器 在图1中选中“链接服务器”,右键选择“新建链接服务器”,如图2,配置相关参数。2、配置相关参数 在“常规”选项中,填写“链接...比如我们建立了链接服务器ORADBCONN,而Oracle中在HRSOFT用户下
SQLSERVER数据库中存储BLOB类型数据,在线等现在公司要求用PB6.5+SQLser
例如,列的数据类型为int,插入的数据是‘aaa’就会出错。当数据为字符型时要用单引号括起。 (2)DEFAULT:指定为该列的默认值。前提是该列原先已经指定了默认值。 如果列清单和VALUES清单都为空,则INSERT会创建一...
1主要针对连接oracle数据库代码 2解决[Err] ORA-01830: date format picture ends before converting entire input string报错问题代码,主要是将timestamp数据类型进行格式化成正常数据
不懂这块的看看啦,mysql中中四中时间类型之一的TimeStamp,在项目中的简单运用,如何将TimeStamp转换成自己需要的时间格式