JDBC ResultSet:我需要一个getDateTime,但只有getDate和getTimeStamp [英] JDBC ResultSet: I need a getDateTime, but there is only getDate and getTimeStamp

查看:195
本文介绍了JDBC ResultSet:我需要一个getDateTime,但只有getDate和getTimeStamp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从带有JDBC的Oracle DB表中获取DATETIME列。这是我的代码:

I would like to get the DATETIME column from an Oracle DB Table with JDBC. Here is my code:

int columnType = rsmd.getColumnType(i);
if(columnType == Types.DATE)
{
    Date aDate = rs.getDate(i);
    valueToInsert = aDate.toString();
}
else if(columnType == Types.TIMESTAMP)
{
    Timestamp aTimeStamp = rs.getTimestamp(i);
    valueToInsert = aTimeStamp.toString();
}
else
{
    valueToInsert = rs.getString(i);
}

我必须先识别列类型。我感兴趣的字段被识别为Types.DATE,但它实际上是数据库中的DATETIME,因为它具有以下格式:07.05.2009 13:49:32

I have to identify the column type first. The field I am interested in is recognized as a Types.DATE, but it is really a DATETIME in the DB since it has this format: "07.05.2009 13:49:32"

getDate截断时间:07.05.2009
和getString将.0追加到它:07.05.2009 13:49:32.0

getDate truncates the time: "07.05.2009" and getString appends ".0" to it: "07.05.2009 13:49:32.0"

当然我可以删除最终的.0并一直使用getString,但这是一个肮脏的解决方法。

Of course I could just remove the final .0 and work with getString all the time, but it is a dirty workaround.

任何想法?我正在寻找一个getDateTime方法。

Any ideas ? I was looking for a getDateTime method.

干杯,
Tim

Cheers, Tim

推荐答案

java.util.Date date;
Timestamp timestamp = resultSet.getTimestamp(i);
if (timestamp != null)
    date = new java.util.Date(timestamp.getTime()));

然后按照您喜欢的方式对其进行格式化。

Then format it the way you like.

这篇关于JDBC ResultSet:我需要一个getDateTime,但只有getDate和getTimeStamp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆