如何从JAVA中的ResultSet或ResultSetMetaData对象获取数据库表主键的列名?

发布于 2024-08-21 00:35:24 字数 135 浏览 4 评论 0原文

我正在编写一个 Java 应用程序。我有一个结果集。现在我想找出表的主键的列名。

是否可以通过 ResultSet 对象或 ResultSetMetaData 对象或任何其他方式获取该列名称。

我没有找到任何方法来找到这个。

I am writing a Java Application. I have got a ResultSet. Now I want to find out the coloumn name of the primary key of the table.

Is it possible to get that coloumn name through ResultSet object or ResultSetMetaData Object or any other way.

I didn't find any way to find this.

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

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

发布评论

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

评论(3

冷…雨湿花 2024-08-28 00:35:24

不可以。您不会从 ResultSetResultSetMetadata 获取该信息。

您想要使用的是 DatabaseMetadata 类。从该类中检查 getPrimaryKeys 方法以获取所需的信息。

当然,要使用它,您需要知道表的名称。

No. You will not get that information from ResultSet or ResultSetMetadata.

What you want to use for that is DatabaseMetadata class. From that class check getPrimaryKeys method to get the information you want.

Of course, to use this, you will need to know the name of the table.

百思不得你姐 2024-08-28 00:35:24

我想补充一点,如果表中有自增字段,它一定是主键。因此,您的代码可能如下所示:

if(metaColumn.isAutoIncrement(i)) {
  primaryKey = metaColumn.getColumnName(i);
  i=nColoumn+1;
}

它使用 ResultSetMetaData

I want to add, if in a table you have autoincrement field, it must be the primary key. So, your code could look like this:

if(metaColumn.isAutoIncrement(i)) {
  primaryKey = metaColumn.getColumnName(i);
  i=nColoumn+1;
}

It uses ResultSetMetaData.

dbVisualizer 是一个很好的工具,可以用来验证元数据信息。

它使用 JDBC 元数据来检索表定义和数据库的其他部分。架构和目录是表定义视图中的列 - 因此您可以检查您喜欢的数据库的这些列中包含哪些值。

dbVisualizer 有免费的基本版本。

A nice tool which you can use to verify the metadata information is dbVisualizer.

It uses the JDBC metadata to retrieve table definitions and other parts of the database. Schema and Catalog are columns in the table definition view - so you can check which values are in these columns for your favorite database.

dbVisualizer is available in a free basic version.

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