用JDBC取到数据库表之间的主外键关系

java.sql.DatabaseMetaData   metadata   =   db.getMetaData();//db为一个connection
ResultSet   rs   =   metadata.getExportedKeys(null,   null,   tablename);//获得某个表的metadata
String   foreigntablename   =   rs.getString(7);//从   1到16   依次试试,就知道各个的含义了,另外也可以自己直接查api,看是什么...     7是外键的表名,其他有字段名等等

DatabaseMetaData的
getExportedKeys(String   catalog,   String   schema,   String   table)  
                    Retrieves   a   description   of   the   foreign   key   columns   that   reference   the   given   table 's   primary   key   columns   (the   foreign   keys   exported   by   a   table).

getImportedKeys(String   catalog,   String   schema,   String   table)  
                    Retrieves   a   description   of   the   primary   key   columns   that   are   referenced   by   a   table 's   foreign   key   columns   (the   primary   keys   imported   by   a   table).

猜你喜欢

转载自harveyzeng.iteye.com/blog/1600715