123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- package com.hcks.cmfds.commons.util;
- import java.sql.Connection;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.HashSet;
- import java.util.Set;
- import javax.sql.DataSource;
- import org.apache.log4j.Logger;
- import com.hcks.cmfds.core.exception.JdbcDaoException;
- /**
- *
- *
- * @Author 刘厦 (liusha.information@gmail.com)
- * @Date 创建时间:2011-7-19,上午10:06:06
- * @Version 0.0.0
- *
- * 类说明:
- *
- */
- public class DataBaseHelper {
-
- protected final Logger logger = Logger.getLogger(DataBaseHelper.class);
-
- /**
- * 获取数据库表
- * @param dataSource
- * @return
- */
- public static Set<String> getTablesByDataSource(DataSource dataSource){
- Set<String> tables = new HashSet<String>();
- ResultSet _result = null;
- Connection conn = null;
- try {
- conn = dataSource.getConnection();
- _result = conn.getMetaData().getTables(null, "%", "%", new String[]{"TABLE"});
- while (_result.next()) {
- String tableName = _result.getString("TABLE_NAME");
- tables.add(tableName);
- }
- } catch (SQLException e) {
- try {
- conn.close();
- } catch (SQLException e1){
- e.printStackTrace();
- }
- throw new JdbcDaoException("获取元数据错误!");
- } finally{
- _result = null;
- try {
- conn.close();
- } catch (SQLException e2){
- e2.printStackTrace();
- }
- }
-
- return tables;
- }
-
- /**
- * 获取表的主键集合
- * @param tableName
- * @return
- */
- public static Set<String> getTablePrimaryKeyFieldNames(String tableName,DataSource dataSource) {
- Set<String> pks = new HashSet<String>();
- ResultSet _result = null;
- Connection conn = null;
- try {
- conn = dataSource.getConnection();
- _result = conn.getMetaData().getPrimaryKeys(null, null, tableName);
- while (_result.next()) {
- pks.add(_result.getString(4));
- }
- } catch (SQLException e) {
- try {
- conn.close();
- } catch (SQLException e1){
- e.printStackTrace();
- }
- throw new JdbcDaoException("获取元数据错误!");
- } finally{
- _result = null;
- try {
- conn.close();
- } catch (SQLException e2){
- e2.printStackTrace();
- }
- }
- return pks;
- }
-
- /**
- * 获取表的所有字段
- * @param tableName
- * @return
- */
- public static Set<String> getTableFieldNames(String tableName,DataSource dataSource){
- Set<String> columns = new HashSet<String>();
- ResultSet _result = null;
- Connection conn = null;
- try {
- conn = dataSource.getConnection();
- _result = conn.getMetaData().getColumns(null, "%", tableName, "%");
- while (_result.next()) {
- String columnName = _result.getString("COLUMN_NAME");
- columns.add(columnName);
- }
- } catch (SQLException e) {
- try {
- conn.close();
- } catch (SQLException e1){
- e.printStackTrace();
- }
- throw new JdbcDaoException("获取元数据错误!");
- } finally{
- _result = null;
- try {
- conn.close();
- } catch (SQLException e2){
- e2.printStackTrace();
- }
- }
- return columns;
- }
-
-
-
-
-
- /**
- * 判断表是否是单主键
- * @param tableName
- * @return
- */
- public boolean isSinglePk(String tableName,DataSource dataSource){
- return getTablePrimaryKeyFieldNames(tableName,dataSource).size() == 1;
- }
- }
|