如何检查CachedRowSet中是否存在列名? [英] How do I check to see if a column name exists in a CachedRowSet?

查看:250
本文介绍了如何检查CachedRowSet中是否存在列名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看可能会发生变化的视图中的数据。在我执行 crs.get ******()之前我需要知道列是否存在。我发现我可以查询这样的元数据来查看如果在我请求数据之前存在一列。

I am querying data from views that are subject to change. I need to know if the column exists before I do a crs.get******().I have found that I can query the metadata like this to see if a column exist before I request the data from it.

ResultSetMetaData meta = crs.getMetaData();
int numCol = meta.getColumnCount();

for (int i = 1; i < numCol+1; i++) 
    if(meta.getColumnName(i).equals("name"))
        return true;

是否有更简单的方法来检查列是否存在?

Is there a simpler way of checking to see if a column exists?

编辑:
必须与数据库无关。这就是我引用 CachedRowSet 而不是数据库的原因。

It must be database agnostic. That is why I am referencing the CachedRowSet instead of the database.

推荐答案

使用通用JDBC API没有更简单的方法(至少不是我所知道的,或者可以找到......我在我自己开发的工具集中有完全相同的代码。)

There's not a simpler way with the general JDBC API (at least not that I know of, or can find...I've got exactly the same code in my home-grown toolset.)

(您的代码未完成):

ResultSetMetaData meta = crs.getMetaData();
 int numCol = meta.getColumnCount();

for (int i = 1; i < numCol+1; i++) 
{
    if(meta.getColumnName(i).equals("name"))
    {return true;}

}
return false;

话虽这么说,如果你使用专有的,特定于数据库的API和/或SQL查询,我'我相信你可以找到更优雅的方法来做同样的事情......但你必须为你需要处理的每个数据库编写自定义代码。如果我是你,我会坚持使用JDBC API。

That being said, if you use proprietary, database-specific API's and/or SQL queries, I'm sure you can find more elegant ways of doing the same thing...but you'd have to write custom code for each database you need to deal with. I'd stick with the JDBC APIs, if I were you.

您提出的解决方案是否存在让您认为不正确的问题?这对我来说似乎很简单......

Is there something about your proposed solution that makes you think it's incorrect? It seems simple enough to me...

这篇关于如何检查CachedRowSet中是否存在列名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆