天天看点

使用JDBC查询表的主键

最近在做重构数据库表管理工作,今天遇到一个关于[给定表名称]取它的主键并进行其它关联操作.

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. }