• JDBC获取数据库信息


    当同一个项目部署到不同数据库环境时,程序中涉及sql语句执行时,由于各种数据库的差异,可能会产生异常。则就需要获取数据库信息做适当处理。

      1 public class DBUtils {
      2     private static Log logger = LogFactory.getLog(DBUtils.class);
      3 
      4     private static String dbName;
      5 
      6     private static final String DBTYPE_DB2 = "DB2";
      7 
      8     private static final String DBTYPE_SYBASE = "Sybase";
      9 
     10     private static final String DBTYPE_ORACLE = "Oracle";
     11 
     12     private static final String DBTYPE_SQLSERVER = "SqlServer";
     13 
     14     private static final String DBTYPE_MYSQL = "MySQL";
     15 
     16 /*    private static Cache dbTypeJudgeCache = DBCache.DBTypeJudgeCache;*/
     17 
     18     private static String getDatabaseProductName() {
     19         WebApplicationContext webContext = (WebApplicationContext)ComponentFactory.getApplicationContext();
     20         DataSource ds = (DataSource) webContext.getBean("dataSource");
     21         if (dbName != null) {
     22             return dbName;
     23         }
     24         Connection con = null;
     25         try {
     26             con = DataSourceUtils.getConnection(ds);
     27         } catch (DataAccessException ex) {
     28             logger.error("获取数据库连接失败", ex);
     29         }
     30 
     31         if (con != null) {
     32             try {
     33                 DatabaseMetaData dbmd = con.getMetaData();
     34                 if (dbmd != null) {
     35                     dbName = dbmd.getDatabaseProductName();
     36                     if (dbName != null && dbName.startsWith("DB2/")) {
     37                         dbName = DBUtils.DBTYPE_DB2;
     38                     } else if (dbName != null && dbName.startsWith("Oracle")) {
     39                         dbName = DBUtils.DBTYPE_ORACLE;
     40                     } else if (dbName != null
     41                             && dbName.startsWith("Microsoft SQL Server")) {
     42                         dbName = DBUtils.DBTYPE_SQLSERVER;
     43                     } else if (dbName != null
     44                             && (dbName.equals("Adaptive Server Enterprise") || dbName
     45                                     .equals("Adaptive Server Anywhere"))) {
     46                         dbName = DBUtils.DBTYPE_SYBASE;
     47                     } else if (dbName != null && dbName.startsWith("MySQL")) {
     48                         dbName = DBUtils.DBTYPE_MYSQL;
     49                     }
     50                 } else {
     51                     logger.warn("获取数据库类型出错");
     52                 }
     53             } catch (SQLException se) {
     54                 logger.error("获取数据库类型出错", se);
     55             } finally {
     56                 DataSourceUtils.closeConnectionIfNecessary(con, ds);
     57             }
     58         }
     59         return dbName;
     60     }
     61 
     62     private static String getDbType() {
     63         String dbType = null;
     64         try {
     65             dbType = DBUtils.getDatabaseProductName();
     66         } catch (Exception e) {
     67             // 异常不处理;
     68         }
     69         if (dbType == null) {
     70             dbType = "";
     71         } else {
     72             dbType = dbType.trim();
     73         }
     74         return dbType;
     75     }
     76 
     77     // 下面四个方法最好能有更好的办法使每次进来的时候只比较一次,不用每次进来都比较
     78     public static boolean dbTypeIsDB2() {
     79         Boolean is_db2 = new Boolean(false);
     80         logger.debug("使用CACHE前 " + is_db2);
     81         is_db2 = new Boolean(DBUtils.getDbType().equalsIgnoreCase(DBUtils.DBTYPE_DB2));
     82         return is_db2.booleanValue();
     83     }
     84 
     85     public static boolean dbTypeIsORACLE() {
     86         Boolean is_oracle = new Boolean(false);
     87         is_oracle = new Boolean(DBUtils.getDbType().equalsIgnoreCase(DBUtils.DBTYPE_ORACLE));
     88         return is_oracle.booleanValue();
     89     }
     90 
     91     public static boolean dbTypeIsSYBASE() {
     92         Boolean is_sybase = new Boolean(false);
     93         is_sybase = new Boolean(DBUtils.getDbType().equalsIgnoreCase(DBUtils.DBTYPE_SYBASE));
     94         return is_sybase.booleanValue();
     95     }
     96 
     97     public static boolean dbTypeIsSQLSERVER() {
     98         Boolean is_sqlserver = new Boolean(false);
     99         is_sqlserver = new Boolean(DBUtils.getDbType().equalsIgnoreCase(DBUtils.DBTYPE_SQLSERVER));
    100         return is_sqlserver.booleanValue();
    101     }
    102 
    103     public static boolean dbTypeIsMYSQL() {
    104         Boolean is_mysql = new Boolean(false);
    105         is_mysql = new Boolean(DBUtils.getDbType().equalsIgnoreCase(DBUtils.DBTYPE_MYSQL));
    106         return is_mysql.booleanValue();
    107     }
    108 }
  • 相关阅读:
    江の島西浦写真館2-1
    江の島西浦写真館1-2
    Oracle 查询表空间使用情况
    Oracle 的开窗函数 rank,dense_rank,row_number
    oracle11G 用户密码180天修改概要文件过程
    CentOS6 安装 MySQL5.7
    linux下SS 网络命令详解
    CentOS6 网络设置
    redhat 6 红帽6 Linux 网络配置
    Oracle分析函数——函数列表
  • 原文地址:https://www.cnblogs.com/mayt-/p/3445496.html
Copyright © 2020-2023  润新知