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

别来无恙 提交于 2020-01-04 14:34:52

问题


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.

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.

If I have to use the database specific metadata queries, which HSQLDB internal metadata tables hold that information?


回答1:


Your best bet is Connection#getMetaData() which returns DatabaseMetaData with all methods to obtain information about all tables, columns, primary keys, foreign keys, etcetera. You're however dependent on the JDBC implementation (read: the JDBC driver make/version) whether this is fully supported.




回答2:


JDBC does have support for this. Check out DatabaseMetaData.getCrossReference class. 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 (i.e. Oracle) to optimize the calls. Don't know if HQLSB requires this.



来源:https://stackoverflow.com/questions/2329588/how-do-i-get-name-of-the-target-table-and-column-of-foreign-key-column-with-plai

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!