最近在做重構資料庫表管理工作,今天遇到一個關于[給定表名稱]取它的主鍵并進行其它關聯操作.
1. public String[] getTablePK(String tableCodes) throws Exception {
2. null;
3. null;
4. null;
5. null;
6. try {
7. /** 查了下API,ResultSet 會包含五列,分别是:
8. * TABLE_CAT 表目錄一般會是NULL
9. * TABLE_SCHEM 表索引,主鍵所在表的架構,通常會設定為dbmd.getUserName()
10. * TABLE_NAME 包含主鍵的表名稱
11. * COLUMN_NAME 主鍵名稱
12. * KEY_SEQ 主鍵序列
13. * PK_NAME 限制名稱
14. */
15. conn = dataSource.getConnection();
16. dbmd = conn.getMetaData();
17. null, null, tableCodes);
18. "";
19. while (rs.next()) {
20. "COLUMN_NAME") + ",";
21. }
22. ",");
23. if (tempPK.length() < 1) {
24. null;
25. }
26. catch(SQLException sqle) {
27. logger.error(sqle);
28. finally {
29. if (rs != null) {
30. try{
31. rs.close();
32. catch(SQLException sqle) {
33. logger.error(sqle);
34. }
35. }
36. if (conn != null) {
37. try {
38. catch(SQLException sqle) {
39. logger.error(sqle);
40. }
41. }
42. }
43. return resultArray;
44. }