当前位置: 技术问答>java相关
有关java.sql.ResultSet 利用SetDate往 oracle 中插入时间和日期的问题!!
来源: 互联网 发布时间:2015-05-12
本文导语: Calendar cc = Calendar.getInstance(Locale.CHINA); //1980-5-5 10:35:56 cc.set(1980,4 ,5,10,35,56); 我想将这个时间写入数据库。请问如何实现,我用setDate(),只能插入日期,"1980-5-5",时间就丢掉了,为什么呢? ...
Calendar cc = Calendar.getInstance(Locale.CHINA);
//1980-5-5 10:35:56
cc.set(1980,4 ,5,10,35,56);
我想将这个时间写入数据库。请问如何实现,我用setDate(),只能插入日期,"1980-5-5",时间就丢掉了,为什么呢?
谢谢大家了。
//1980-5-5 10:35:56
cc.set(1980,4 ,5,10,35,56);
我想将这个时间写入数据库。请问如何实现,我用setDate(),只能插入日期,"1980-5-5",时间就丢掉了,为什么呢?
谢谢大家了。
|
ps.setTimestamp(1,new Timestamp(cc.getTime().getTime()));
|
setTimestamp(int parameterIndex, Timestamp x, Calendar cal)
Sets the designated parameter to the given java.sql.Timestamp value, using the given Calendar object. The driver uses the Calendar object to construct an SQL TIMESTAMP value, which the driver then sends to the database. With a a Calendar object, the driver can calculate the timestamp taking into account a custom timezone. If no Calendar object is specified, the driver uses the default timezone, which is that of the virtual machine running the application.
Sets the designated parameter to the given java.sql.Timestamp value, using the given Calendar object. The driver uses the Calendar object to construct an SQL TIMESTAMP value, which the driver then sends to the database. With a a Calendar object, the driver can calculate the timestamp taking into account a custom timezone. If no Calendar object is specified, the driver uses the default timezone, which is that of the virtual machine running the application.
|
可以直接用字符串插入日期时间,to_date('1980-4-5 10:35:56', 'yyyy-mm-dd hh24:mi:ss')就可以了,时间也很容易直接转换成字符串:
SimpleDateFormat simDate = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
java.util.Date collectTime = new java.util.Date();
String strCollectTime = simDate.format(collectTime );
SimpleDateFormat simDate = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
java.util.Date collectTime = new java.util.Date();
String strCollectTime = simDate.format(collectTime );