简体   繁体   English

如何使用普通JDBC获得目标表的名称和外键列的列

[英]How do I get name of the target table and column of foreign key column with plain JDBC

I'm trying to make a piece of code using plain JDBC that fetches me the name of both target table and column of a foreign key of a specific column in specific table but going through the core interfaces I can't seem to find a direct way to do this. 我正在尝试使用纯JDBC编写一段代码,该代码既可以获取目标表的名称,又可以获取特定表中特定列的外键的列,但是通过核心接口,我似乎找不到直接的做到这一点的方法。

Is there a way to get such information about foreign keys through JDBC directly or do I have to resort to metadata queries to specific database, in this case HSQLDB. 有没有一种方法可以直接通过JDBC获得有关外键的此类信息,还是我必须求助于特定数据库(在这种情况下为HSQLDB)的元数据查询。

If I have to use the database specific metadata queries, which HSQLDB internal metadata tables hold that information? 如果必须使用特定于数据库的元数据查询,则哪个HSQLDB内部元数据表保存该信息?

Your best bet is Connection#getMetaData() which returns DatabaseMetaData with all methods to obtain information about all tables , columns , primary keys , foreign keys , etcetera. 最好的选择是Connection#getMetaData() ,它使用所有方法返回DatabaseMetaData以获得有关所有主键外键等的信息。 You're however dependent on the JDBC implementation (read: the JDBC driver make/version) whether this is fully supported. 但是,是否完全支持JDBC实现(请阅读:JDBC驱动程序版本)。

JDBC does have support for this. JDBC确实对此提供支持。 Check out DatabaseMetaData.getCrossReference class. 签出DatabaseMetaData.getCrossReference类。 Other methods on DatabaseMetdata support for querying schema, catalog, tables, columns, etc. Bear in mind some databases require extra parameters on your URL to turn on Metadata (ie Oracle) to optimize the calls. DatabaseMetdata上的其他方法支持查询架构,目录,表,列等。请记住,某些数据库需要URL上具有额外的参数才能打开元数据(即Oracle)以优化调用。 Don't know if HQLSB requires this. 不知道HQLSB是否需要这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM