当同一个项目部署到不同数据库环境时,程序中涉及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 }