[英]How can i get whole date and time data when using cachedrowset from Oracle date field
我从带有结果集的 oracle 获取数据,然后将它们填充到cachedrowset
。 但是当我在数据库中获取日期字段并打印它时,它只打印日期。 我检查了 api 并知道getDate()
方法返回java.sql.date
,并且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);
}
执行查询方法
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
如果使用 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
希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.