测试 ResultSet 以查看它是否包含列标签

发布于 2024-10-01 16:38:58 字数 334 浏览 0 评论 0原文

是否可以检查 ResultSet 以查看它是否包含当前行的列标签。我假设它是由某种 Map 支持的,因此提供 rs.contains("label") 方法不会那么昂贵,但我在 javadoc 中找不到。

我当前测试标签是否存在的方法是:

BigDecimal bid;
    try {
        bid = rs.getBigDecimal("bid");
    }
    catch(final SQLException e) {
        bid = null;
    }

但这对我来说似乎很不整洁,如果您想以这种方式测试多行,则将无法读取。

Is it possible to check a ResultSet to see if it contains a column label for the current row. I would assume that it is backed by some kind of Map, so providing an rs.contains("label") method can't be that expensive, but I can't find one in the javadoc.

The current way I'm testing for the presence of a label is:

BigDecimal bid;
    try {
        bid = rs.getBigDecimal("bid");
    }
    catch(final SQLException e) {
        bid = null;
    }

But this seems untidy to me, and will be unreadable if you want to test multiple rows in this manner.

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

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

发布评论

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

评论(3

恋竹姑娘 2024-10-08 16:38:58

您可以使用从结果集中获取的元数据。使用 rs.getMetaData(),然后在元数据上有 getColumnName()getColumnLabel() 方法。

You can use metadata that you can obtain from resultset. Use rs.getMetaData(), and then on metadata there are getColumnName() and getColumnLabel() methods.

雨夜星沙 2024-10-08 16:38:58

您可以使用 ResultSetMetaData (请参阅我的相关帖子 这里)。这就是我希望你使用的。您要做的就是从 ResultSet 中检索 ResultSetMetaData,迭代它并查看 ResultSetMetaData.getColumnName(int index) 是否返回您一直在查找的列名称。

或者,您可以使用 ResultSet.findColumn() 在 ResultSet 中查找列。

You can use ResultSetMetaData (see my related post here). This is what I would prefer you to use. What you do is retrieve the ResultSetMetaData from the ResultSet, iterate through it and see if ResultSetMetaData.getColumnName(int index) returns the column name you've been looking for.

Alternatively, you can use ResultSet.findColumn() to find a column within a ResultSet.

也只是曾经 2024-10-08 16:38:58

为什么?您的查询将已经确定返回哪些列。但 ResultSetMetadata 会告诉您存在哪些列。

Why? Your query will already determine what columns are returned. But the ResultSetMetadata tells you what columns are present.

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