/***
* 獲取表的主從資訊
* @param tableName
* @param conn
* @throws SQLException
* @throws IOException
* @throws ClassNotFoundException
*/
public static List<String[]> getForginKeyInfos(String tableName,
Connection conn) throws SQLException, IOException,
ClassNotFoundException {
List<String[]> forginKeyInfoList = new ArrayList<String[]>();
Statement stmt = conn.createStatement();
String sql = "select * from " + tableName;
ResultSet set = stmt.executeQuery(sql);
if (set != null) {
ResultSet rst = conn.getMetaData().getImportedKeys(null, null,
tableName);
if (rst != null) {
String[] pf = new String[3];
while (rst.next()) {
pf = new String[3];
pf[0] = rst.getString(3); //主表表名
pf[1] = rst.getString(4); //外鍵在主表的欄位名
pf[2] = rst.getString(8); //外鍵在從表的欄位名
forginKeyInfoList.add(pf);
}
}
rst.close();
}
set.close();
stmt.close();
return forginKeyInfoList;

 

}
arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()