9

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.

4

3 回答 3

9

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.

于 2010-02-06T12:01:54.477 回答
3

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

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

它使用ResultSetMetaData.

于 2011-03-25T13:04:44.107 回答
0

可用于验证元数据信息的一个不错的工具是dbVisualizer

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

dbVisualizer 提供免费的基本版本。

于 2010-02-06T18:23:16.113 回答