如何正确地将 SQL 日期时间返回到我的 Java 应用程序中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5319570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I properly get a SQL Datetime back into my Java application?
提问by Raven Dreamer
I'm having trouble with retrieving queries from my SQL database. I can get the blasted things addedto the database, but I'm having an inordinate amount of difficulties performing the reverse. Three things, in order:
我在从 SQL 数据库检索查询时遇到问题。我可以将这些糟糕的东西添加到数据库中,但是我在执行相反的操作时遇到了过多的困难。三件事,按顺序:
The SQL Table itself:
SQL 表本身:
CREATE TABLE patientInstructions (
id INT UNSIGNED AUTO_INCREMENT,
lastUpdated datetime NOT NULL,
PatientID BIGINT UNSIGNED NOT NULL,
HCPID BIGINT UNSIGNED NOT NULL,
OVid BIGINT UNSIGNED NOT NULL,
urlLink VARCHAR(250) NOT NULL,
linkInstructions VARCHAR(500) NOT NULL,
linkName VARCHAR(100) NOT NULL,
PRIMARY KEY (id)
) AUTO_INCREMENT=1 ENGINE=MyISAM;
The method call that is failing (I'm getting -1L instead of the actual data value stored in the database, which is why I know there's a problem in the first place):
失败的方法调用(我得到的是 -1L 而不是存储在数据库中的实际数据值,这就是为什么我首先知道存在问题):
public String getLastUpdated(long ovID) throws DBException {
try {
return psiDAO.getLastUpdated(ovID);
} catch (myOwnException e) {
e.printStackTrace();
return "-1L";
}
}
And finally, the method call which is failing:
最后,失败的方法调用:
public String getLastUpdated(long ovId) throws DBException {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = factory.getConnection();
ps = conn.prepareStatement("SELECT * FROM patientInstructions"
+ " WHERE ovId=?");
ps.setLong(1, ovId);
ResultSet rs = ps.executeQuery();
java.util.Date updated = new java.util.Date();
updated = rs.getTime("lastUpdated");
return updated.toString();
} catch (SQLException e) {
e.printStackTrace();
throw new DBException(e);
} finally {
DBUtil.closeConnection(conn, ps);
}
}
What Java object matches a SQL Datetime? I've tried rs.getTimestamp, rs.getDate, etc. but haven't had any more success (though I'm not ruling out that I botched those up either). Am I making a mistake transferring the data from the resultset back to Java object?
什么 Java 对象与 SQL 日期时间匹配?我尝试过 rs.getTimestamp、rs.getDate 等,但没有取得更大的成功(虽然我也不排除我把它们搞砸了)。我是否在将数据从结果集传输回 Java 对象时犯了错误?
采纳答案by Ernest Friedman-Hill
You mustcall ResultSet.next() on a ResultSet before accessing each row, including the first. You're not doing that here; the exception message should actually be telling you this.
在访问每一行(包括第一行)之前,您必须对 ResultSet 调用 ResultSet.next()。你不是在这里做的;异常消息实际上应该告诉您这一点。
回答by David Conrad
There are three classes: java.sql.Date, java.sql.Time, and java.sql.Timestamp. I'm not sure which one most closely corresponds to a DATETIME in SQL. All three derive from java.util.Date, which is why you are able to assign the result of calling rs.getTime to one.
共有三个类:java.sql.Date、java.sql.Time 和 java.sql.Timestamp。我不确定哪一个最接近 SQL 中的 DATETIME。这三个都派生自 java.util.Date,这就是为什么您可以将调用 rs.getTime 的结果分配给一个。
I don't have a setup here that would allow me to test it, but I would try using one of the types from java.sql.* and see what results that gives you.
我这里没有设置可以让我测试它,但我会尝试使用 java.sql.* 中的一种类型,看看会给你什么结果。
Whoops!I posted my answer before Ernest's and I didn't even notice the missing rs.next(). That is definitely your problem, and I bet java.util.Date will work just fine for you, with that change.
哎呀!我在 Ernest 之前发布了我的答案,我什至没有注意到缺少 rs.next()。这绝对是你的问题,我敢打赌 java.util.Date 对你来说会很好,有了这个改变。
回答by limc
Since your datatype is datetime
, you will need to use getTimestamp()
... see here for more datatype mapping information.
由于您的数据类型是datetime
,您将需要使用getTimestamp()
...查看此处了解更多数据类型映射信息。
So, your code should look something like this:-
所以,你的代码应该是这样的:-
...
ResultSet rs = ps.executeQuery();
String updated = "";
if (rs.next()) {
updated = rs.getTimestamp("lastUpdated").toString();
}
rs.close();
ps.close();
return updated;
回答by Piyush Mattoo
Please paste the exception here. You need to call ResultSet's next method before retrieving the values from the result set. Also, try java.sql.Timestamp lastUpdatedTimestamp = getTimestamp("lastUpdated")
If needed , you can convert timestamp to String later.
请在此处粘贴异常。在从结果集中检索值之前,您需要调用 ResultSet 的 next 方法。另外,尝试java.sql.Timestamp lastUpdatedTimestamp = getTimestamp("lastUpdated")
如果需要,您可以稍后将时间戳转换为字符串。