DataBaseHelper.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package com.hcks.cmfds.commons.util;
  2. import java.sql.Connection;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.util.HashSet;
  6. import java.util.Set;
  7. import javax.sql.DataSource;
  8. import org.apache.log4j.Logger;
  9. import com.hcks.cmfds.core.exception.JdbcDaoException;
  10. /**
  11. *
  12. *
  13. * @Author 刘厦 (liusha.information@gmail.com)
  14. * @Date 创建时间:2011-7-19,上午10:06:06
  15. * @Version 0.0.0
  16. *
  17. * 类说明:
  18. *
  19. */
  20. public class DataBaseHelper {
  21. protected final Logger logger = Logger.getLogger(DataBaseHelper.class);
  22. /**
  23. * 获取数据库表
  24. * @param dataSource
  25. * @return
  26. */
  27. public static Set<String> getTablesByDataSource(DataSource dataSource){
  28. Set<String> tables = new HashSet<String>();
  29. ResultSet _result = null;
  30. Connection conn = null;
  31. try {
  32. conn = dataSource.getConnection();
  33. _result = conn.getMetaData().getTables(null, "%", "%", new String[]{"TABLE"});
  34. while (_result.next()) {
  35. String tableName = _result.getString("TABLE_NAME");
  36. tables.add(tableName);
  37. }
  38. } catch (SQLException e) {
  39. try {
  40. conn.close();
  41. } catch (SQLException e1){
  42. e.printStackTrace();
  43. }
  44. throw new JdbcDaoException("获取元数据错误!");
  45. } finally{
  46. _result = null;
  47. try {
  48. conn.close();
  49. } catch (SQLException e2){
  50. e2.printStackTrace();
  51. }
  52. }
  53. return tables;
  54. }
  55. /**
  56. * 获取表的主键集合
  57. * @param tableName
  58. * @return
  59. */
  60. public static Set<String> getTablePrimaryKeyFieldNames(String tableName,DataSource dataSource) {
  61. Set<String> pks = new HashSet<String>();
  62. ResultSet _result = null;
  63. Connection conn = null;
  64. try {
  65. conn = dataSource.getConnection();
  66. _result = conn.getMetaData().getPrimaryKeys(null, null, tableName);
  67. while (_result.next()) {
  68. pks.add(_result.getString(4));
  69. }
  70. } catch (SQLException e) {
  71. try {
  72. conn.close();
  73. } catch (SQLException e1){
  74. e.printStackTrace();
  75. }
  76. throw new JdbcDaoException("获取元数据错误!");
  77. } finally{
  78. _result = null;
  79. try {
  80. conn.close();
  81. } catch (SQLException e2){
  82. e2.printStackTrace();
  83. }
  84. }
  85. return pks;
  86. }
  87. /**
  88. * 获取表的所有字段
  89. * @param tableName
  90. * @return
  91. */
  92. public static Set<String> getTableFieldNames(String tableName,DataSource dataSource){
  93. Set<String> columns = new HashSet<String>();
  94. ResultSet _result = null;
  95. Connection conn = null;
  96. try {
  97. conn = dataSource.getConnection();
  98. _result = conn.getMetaData().getColumns(null, "%", tableName, "%");
  99. while (_result.next()) {
  100. String columnName = _result.getString("COLUMN_NAME");
  101. columns.add(columnName);
  102. }
  103. } catch (SQLException e) {
  104. try {
  105. conn.close();
  106. } catch (SQLException e1){
  107. e.printStackTrace();
  108. }
  109. throw new JdbcDaoException("获取元数据错误!");
  110. } finally{
  111. _result = null;
  112. try {
  113. conn.close();
  114. } catch (SQLException e2){
  115. e2.printStackTrace();
  116. }
  117. }
  118. return columns;
  119. }
  120. /**
  121. * 判断表是否是单主键
  122. * @param tableName
  123. * @return
  124. */
  125. public boolean isSinglePk(String tableName,DataSource dataSource){
  126. return getTablePrimaryKeyFieldNames(tableName,dataSource).size() == 1;
  127. }
  128. }