如何正确地将 SQL 日期时间返回到我的 Java 应用程序中?

发布于 2024-10-23 11:48:35 字数 1787 浏览 3 评论 0原文

我在从 SQL 数据库检索查询时遇到问题。我可以将这些该死的东西添加到数据库中,但是执行相反的操作却遇到了极大的困难。按顺序排列三件事:

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;

失败的方法调用(我得到 -1L 而不是数据库中存储的实际数据值,这就是为什么我首先知道存在问题):

public String getLastUpdated(long ovID) throws DBException {
        try {
            return psiDAO.getLastUpdated(ovID);
        } catch (myOwnException e) {
            e.printStackTrace();
            return "-1L";
        }
    }

以及最后,失败的方法调用:

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);
        }
    }

哪个 Java 对象与 SQL 日期时间匹配?我尝试过 rs.getTimestamp、rs.getDate 等,但没有取得任何成功(尽管我也不排除我搞砸了这些)。我将数据从结果集传输回 Java 对象时犯了错误吗?

I'm having trouble with retrieving queries from my SQL database. I can get the blasted things added to the database, but I'm having an inordinate amount of difficulties performing the reverse. Three things, in order:

The SQL Table itself:

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):

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

堇色安年 2024-10-30 11:48:35

在访问每一行(包括第一行)之前,您必须对 ResultSet 调用 ResultSet.next()。你不在这里这样做;异常消息实际上应该告诉你这一点。

You must call 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.

始终不够爱げ你 2024-10-30 11:48:35

共有三个类:java.sql.Date、java.sql.Time 和 java.sql.Timestamp。我不确定哪一个与 SQL 中的 DATETIME 最接近。所有三个都派生自 java.util.Date,这就是为什么您能够将调用 rs.getTime 的结果分配给其中之一。

我这里没有允许我测试它的设置,但我会尝试使用 java.sql.* 中的一种类型,看看会给出什么结果。

哎呀!我在 Ernest 之前发布了我的答案,但我什至没有注意到缺少 rs.next()。这绝对是你的问题,我敢打赌 java.util.Date 会在这个改变后为你工作得很好。

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.

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.

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.

负佳期 2024-10-30 11:48:35

由于您的数据类型是datetime,因此您需要使用getTimestamp()...请参阅此处了解更多信息数据类型映射信息

所以,你的代码应该是这样的:-

...
ResultSet rs = ps.executeQuery();

String updated = "";

if (rs.next()) {
    updated = rs.getTimestamp("lastUpdated").toString();
}

rs.close();
ps.close();

return updated;

Since your datatype is datetime, you will need to use getTimestamp()... see here for more datatype mapping information.

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;
多情癖 2024-10-30 11:48:35

请将异常粘贴到此处。在从结果集中检索值之前,您需要调用 ResultSet 的 next 方法。另外,尝试 java.sql.Timestamp lastUpdatedTimestamp = getTimestamp("lastUpdated")
如果需要,您可以稍后将时间戳转换为字符串。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文