gpt4 book ai didi

java - 使用Oracle日期字段中的cachedrowset时如何获取完整的日期和时间数据

转载 作者:行者123 更新时间:2023-11-30 04:36:40 26 4
gpt4 key购买 nike

我从 Oracle 获取数据和结果集,然后将它们填充到 cachedrowset。但是当我在数据库中获取日期字段并打印它时,它只打印日期。我检查了api,知道getDate()方法返回java.sql.date,并且sql.date的时间被转换为0 .我测试如果我调用resultset.getString("datefield"),它可以获取完整的日期和时间数据,那么有没有办法使用cahcedrowset<获取完整的日期和时间数据,谢谢

这是我的源代码

    CachedRowSet rs = null;
try {
rs = executeQuery("select * from t_web_order_result ");
if (rs != null && rs.next()) {
KKLog.info(rs.getDate("stime"));//2012-04-24,it should be 2012-04-24 09:23:33
KKLog.info(rs.getString("stime"));
}
} catch (SQLException e) {
logErr("queryCheckers Err", e);
} finally {
closeCachedRowset(rs);
}

executeQuery方法

    CachedRowSet rowset = null;
Connection connection = null;
PreparedStatement statement = null;
ResultSet rs = null;
try {
connection = getConnection();
statement = connection.prepareStatement(sql);
for (int i = 0; i < params.length; i ++) {
statement.setObject(i + 1, params[i]);
}
rs = statement.executeQuery();
rowset = new CachedRowSetImpl();
rowset.populate(rs);
rs.close();
statement.close();
} finally {
//close....
}
return rowset;

我使用了三种方法来打印日志

    KKLog.info(rs.getDate("stime"));
KKLog.info(rs.getString("stime"));
KKLog.info(rs.getTimestamp("stime"));

日志内容为

    12-11-11 17:06:11 2012-04-24 
12-11-11 17:06:11 2012-04-24
12-11-11 17:06:11 2012-04-24 00:00:00.0

最佳答案

如果你想从数据库获取日期和时间,你必须使用

KKLog.info(rs.getTimestamp("stime"))

这将从表中给出日期和时间,前提是日期和时间存储为 date 数据类型或 timestamp 。另请注意,您必须使用 java.sql.Date 而不是 java.util.Date

更新1

有一些bugs如果使用了cachedrowset,则与从数据库获取时间部分相关。

我尝试使用 CachedRowSet,但时间部分丢失。为了解决这个问题,在数据库连接部分添加以下内容。我也可以看到时间部分。

    java.util.Properties prop = new java.util.Properties();
prop.put("oracle.jdbc.V8Compatible", "true");
prop.put("user", "user");
prop.put("password", "password");
Connection conn =
DriverManager.getConnection("jdbc:oracle:thin:@server:port:SID",prop);

添加prop.put("oracle.jdbc.V8Compatible", "true");是解决这个问题的关键。

我之前的日期和时间

2012-11-08 00:00:00

添加V8Compatible

2012-11-08 13:28:35

希望这有帮助

关于java - 使用Oracle日期字段中的cachedrowset时如何获取完整的日期和时间数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13329391/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com