一、前言
关于ddlutils-1.0.jar开源工具类org.apache.ddlutils.PlatformUtils平台工具类通过数据源连接对象映射常见数据库驱动连接串,从而识别数据库类别DatabaseType,具体参见如下源码说明。
二、源码说明
package org.apache.ddlutils;@b@@b@import java.sql.Connection;@b@import java.sql.DatabaseMetaData;@b@import java.sql.SQLException;@b@import java.util.HashMap;@b@import java.util.Iterator;@b@import java.util.Map.Entry;@b@import java.util.Set;@b@import javax.sql.DataSource;@b@@b@public class PlatformUtils@b@{@b@ public static final String JDBC_DRIVER_DATADIRECT_DB2 = "com.ddtek.jdbc.db2.DB2Driver";@b@ public static final String JDBC_DRIVER_DATADIRECT_SQLSERVER = "com.ddtek.jdbc.sqlserver.SQLServerDriver";@b@ public static final String JDBC_DRIVER_DATADIRECT_ORACLE = "com.ddtek.jdbc.oracle.OracleDriver";@b@ public static final String JDBC_DRIVER_DATADIRECT_SYBASE = "com.ddtek.jdbc.sybase.SybaseDriver";@b@ public static final String JDBC_DRIVER_INET_DB2 = "com.inet.drda.DRDADriver";@b@ public static final String JDBC_DRIVER_INET_ORACLE = "com.inet.ora.OraDriver";@b@ public static final String JDBC_DRIVER_INET_SQLSERVER = "com.inet.tds.TdsDriver";@b@ public static final String JDBC_DRIVER_INET_SYBASE = "com.inet.syb.SybDriver";@b@ public static final String JDBC_DRIVER_INET_POOLED = "com.inet.pool.PoolDriver";@b@ public static final String JDBC_DRIVER_JSQLCONNECT_SQLSERVER = "com.jnetdirect.jsql.JSQLDriver";@b@ public static final String JDBC_DRIVER_JTDS = "net.sourceforge.jtds.jdbc.Driver";@b@ public static final String JDBC_SUBPROTOCOL_DATADIRECT_DB2 = "datadirect:db2";@b@ public static final String JDBC_SUBPROTOCOL_DATADIRECT_SQLSERVER = "datadirect:sqlserver";@b@ public static final String JDBC_SUBPROTOCOL_DATADIRECT_ORACLE = "datadirect:oracle";@b@ public static final String JDBC_SUBPROTOCOL_DATADIRECT_SYBASE = "datadirect:sybase";@b@ public static final String JDBC_SUBPROTOCOL_INET_DB2 = "inetdb2";@b@ public static final String JDBC_SUBPROTOCOL_INET_ORACLE = "inetora";@b@ public static final String JDBC_SUBPROTOCOL_INET_SQLSERVER = "inetdae";@b@ public static final String JDBC_SUBPROTOCOL_INET_SQLSERVER6 = "inetdae6";@b@ public static final String JDBC_SUBPROTOCOL_INET_SQLSERVER7 = "inetdae7";@b@ public static final String JDBC_SUBPROTOCOL_INET_SQLSERVER7A = "inetdae7a";@b@ public static final String JDBC_SUBPROTOCOL_INET_SQLSERVER_POOLED_1 = "inetpool:inetdae";@b@ public static final String JDBC_SUBPROTOCOL_INET_SQLSERVER6_POOLED_1 = "inetpool:inetdae6";@b@ public static final String JDBC_SUBPROTOCOL_INET_SQLSERVER7_POOLED_1 = "inetpool:inetdae7";@b@ public static final String JDBC_SUBPROTOCOL_INET_SQLSERVER7A_POOLED_1 = "inetpool:inetdae7a";@b@ public static final String JDBC_SUBPROTOCOL_INET_SQLSERVER_POOLED_2 = "inetpool:jdbc:inetdae";@b@ public static final String JDBC_SUBPROTOCOL_INET_SQLSERVER6_POOLED_2 = "inetpool:jdbc:inetdae6";@b@ public static final String JDBC_SUBPROTOCOL_INET_SQLSERVER7_POOLED_2 = "inetpool:jdbc:inetdae7";@b@ public static final String JDBC_SUBPROTOCOL_INET_SQLSERVER7A_POOLED_2 = "inetpool:jdbc:inetdae7a";@b@ public static final String JDBC_SUBPROTOCOL_INET_SYBASE = "inetsyb";@b@ public static final String JDBC_SUBPROTOCOL_INET_SYBASE_POOLED_1 = "inetpool:inetsyb";@b@ public static final String JDBC_SUBPROTOCOL_INET_SYBASE_POOLED_2 = "inetpool:jdbc:inetsyb";@b@ public static final String JDBC_SUBPROTOCOL_JSQLCONNECT_SQLSERVER = "JSQLConnect";@b@ public static final String JDBC_SUBPROTOCOL_JTDS_SQLSERVER = "jtds:sqlserver";@b@ public static final String JDBC_SUBPROTOCOL_JTDS_SYBASE = "jtds:sybase";@b@ private HashMap jdbcSubProtocolToPlatform = new HashMap();@b@ private HashMap jdbcDriverToPlatform = new HashMap();@b@@b@ public PlatformUtils()@b@ {@b@ this.jdbcSubProtocolToPlatform.put("axiondb", "Axion");@b@ this.jdbcSubProtocolToPlatform.put("db2j:net", "Cloudscape");@b@ this.jdbcSubProtocolToPlatform.put("cloudscape:net", "Cloudscape");@b@ this.jdbcSubProtocolToPlatform.put("db2", "DB2");@b@ this.jdbcSubProtocolToPlatform.put("db2os390", "DB2");@b@ this.jdbcSubProtocolToPlatform.put("db2os390sqlj", "DB2");@b@ this.jdbcSubProtocolToPlatform.put("as400", "DB2");@b@ this.jdbcSubProtocolToPlatform.put("datadirect:db2", "DB2");@b@ this.jdbcSubProtocolToPlatform.put("inetdb2", "DB2");@b@ this.jdbcSubProtocolToPlatform.put("derby", "Derby");@b@ this.jdbcSubProtocolToPlatform.put("firebirdsql", "Firebird");@b@ this.jdbcSubProtocolToPlatform.put("hsqldb", "HsqlDb");@b@ this.jdbcSubProtocolToPlatform.put("interbase", "Interbase");@b@ this.jdbcSubProtocolToPlatform.put("sapdb", "SapDB");@b@ this.jdbcSubProtocolToPlatform.put("mckoi", "McKoi");@b@ this.jdbcSubProtocolToPlatform.put("microsoft:sqlserver", "MsSql");@b@ this.jdbcSubProtocolToPlatform.put("sqlserver", "MsSql");@b@ this.jdbcSubProtocolToPlatform.put("sqljdbc", "MsSql");@b@ this.jdbcSubProtocolToPlatform.put("datadirect:sqlserver", "MsSql");@b@ this.jdbcSubProtocolToPlatform.put("inetdae", "MsSql");@b@ this.jdbcSubProtocolToPlatform.put("inetdae6", "MsSql");@b@ this.jdbcSubProtocolToPlatform.put("inetdae7", "MsSql");@b@ this.jdbcSubProtocolToPlatform.put("inetdae7a", "MsSql");@b@ this.jdbcSubProtocolToPlatform.put("inetpool:inetdae", "MsSql");@b@ this.jdbcSubProtocolToPlatform.put("inetpool:inetdae6", "MsSql");@b@ this.jdbcSubProtocolToPlatform.put("inetpool:inetdae7", "MsSql");@b@ this.jdbcSubProtocolToPlatform.put("inetpool:inetdae7a", "MsSql");@b@ this.jdbcSubProtocolToPlatform.put("inetpool:jdbc:inetdae", "MsSql");@b@ this.jdbcSubProtocolToPlatform.put("inetpool:jdbc:inetdae6", "MsSql");@b@ this.jdbcSubProtocolToPlatform.put("inetpool:jdbc:inetdae7", "MsSql");@b@ this.jdbcSubProtocolToPlatform.put("inetpool:jdbc:inetdae7a", "MsSql");@b@ this.jdbcSubProtocolToPlatform.put("JSQLConnect", "MsSql");@b@ this.jdbcSubProtocolToPlatform.put("jtds:sqlserver", "MsSql");@b@ this.jdbcSubProtocolToPlatform.put("mysql", "MySQL");@b@ this.jdbcSubProtocolToPlatform.put("oracle:thin", "Oracle");@b@ this.jdbcSubProtocolToPlatform.put("oracle:oci8", "Oracle");@b@ this.jdbcSubProtocolToPlatform.put("oracle:dnldthin", "Oracle");@b@ this.jdbcSubProtocolToPlatform.put("datadirect:oracle", "Oracle");@b@ this.jdbcSubProtocolToPlatform.put("inetora", "Oracle");@b@ this.jdbcSubProtocolToPlatform.put("postgresql", "PostgreSql");@b@ this.jdbcSubProtocolToPlatform.put("sybase:Tds", "Sybase");@b@ this.jdbcSubProtocolToPlatform.put("datadirect:sybase", "Sybase");@b@ this.jdbcSubProtocolToPlatform.put("inetsyb", "Sybase");@b@ this.jdbcSubProtocolToPlatform.put("inetpool:inetsyb", "Sybase");@b@ this.jdbcSubProtocolToPlatform.put("inetpool:jdbc:inetsyb", "Sybase");@b@ this.jdbcSubProtocolToPlatform.put("jtds:sybase", "Sybase");@b@@b@ this.jdbcDriverToPlatform.put("org.axiondb.jdbc.AxionDriver", "Axion");@b@ this.jdbcDriverToPlatform.put("com.ibm.db2.jcc.DB2Driver", "DB2");@b@ this.jdbcDriverToPlatform.put("COM.ibm.db2.jdbc.app.DB2Driver", "DB2");@b@ this.jdbcDriverToPlatform.put("COM.ibm.db2os390.sqlj.jdbc.DB2SQLJDriver", "DB2");@b@ this.jdbcDriverToPlatform.put("com.ibm.as400.access.AS400JDBCDriver", "DB2");@b@ this.jdbcDriverToPlatform.put("com.ddtek.jdbc.db2.DB2Driver", "DB2");@b@ this.jdbcDriverToPlatform.put("com.inet.drda.DRDADriver", "DB2");@b@ this.jdbcDriverToPlatform.put("org.apache.derby.jdbc.EmbeddedDriver", "Derby");@b@ this.jdbcDriverToPlatform.put("org.apache.derby.jdbc.ClientDriver", "Derby");@b@ this.jdbcDriverToPlatform.put("org.firebirdsql.jdbc.FBDriver", "Firebird");@b@ this.jdbcDriverToPlatform.put("org.hsqldb.jdbcDriver", "HsqlDb");@b@ this.jdbcDriverToPlatform.put("interbase.interclient.Driver", "Interbase");@b@ this.jdbcDriverToPlatform.put("com.sap.dbtech.jdbc.DriverSapDB", "SapDB");@b@ this.jdbcDriverToPlatform.put("com.mckoi.JDBCDriver", "McKoi");@b@ this.jdbcDriverToPlatform.put("com.microsoft.jdbc.sqlserver.SQLServerDriver", "MsSql");@b@ this.jdbcDriverToPlatform.put("com.microsoft.sqlserver.jdbc.SQLServerDriver", "MsSql");@b@ this.jdbcDriverToPlatform.put("com.ddtek.jdbc.sqlserver.SQLServerDriver", "MsSql");@b@ this.jdbcDriverToPlatform.put("com.inet.tds.TdsDriver", "MsSql");@b@ this.jdbcDriverToPlatform.put("com.jnetdirect.jsql.JSQLDriver", "MsSql");@b@ this.jdbcDriverToPlatform.put("com.mysql.jdbc.Driver", "MySQL");@b@ this.jdbcDriverToPlatform.put("org.gjt.mm.mysql.Driver", "MySQL");@b@ this.jdbcDriverToPlatform.put("oracle.jdbc.driver.OracleDriver", "Oracle");@b@ this.jdbcDriverToPlatform.put("oracle.jdbc.dnlddriver.OracleDriver", "Oracle");@b@ this.jdbcDriverToPlatform.put("com.ddtek.jdbc.oracle.OracleDriver", "Oracle");@b@ this.jdbcDriverToPlatform.put("com.inet.ora.OraDriver", "Oracle");@b@ this.jdbcDriverToPlatform.put("org.postgresql.Driver", "PostgreSql");@b@ this.jdbcDriverToPlatform.put("com.sybase.jdbc2.jdbc.SybDriver", "Sybase");@b@ this.jdbcDriverToPlatform.put("com.sybase.jdbc.SybDriver", "Sybase");@b@ this.jdbcDriverToPlatform.put("com.ddtek.jdbc.sybase.SybaseDriver", "Sybase");@b@ this.jdbcDriverToPlatform.put("com.inet.syb.SybDriver", "Sybase");@b@ }@b@@b@ public String determineDatabaseType(DataSource dataSource)@b@ throws DatabaseOperationException@b@ {@b@ return determineDatabaseType(dataSource, null, null);@b@ }@b@@b@ public String determineDatabaseType(DataSource dataSource, String username, String password)@b@ throws DatabaseOperationException@b@ {@b@ Connection connection = null;@b@ try@b@ {@b@ if (username != null)@b@ {@b@ connection = dataSource.getConnection(username, password);@b@ }@b@ else@b@ {@b@ connection = dataSource.getConnection();@b@ }@b@@b@ DatabaseMetaData metaData = connection.getMetaData();@b@@b@ String str = determineDatabaseType(metaData.getDriverName(), metaData.getURL());@b@@b@ return str;@b@ }@b@ catch (SQLException ex)@b@ {@b@ }@b@ finally@b@ {@b@ if (connection != null)@b@ {@b@ try@b@ {@b@ connection.close();@b@ }@b@ catch (SQLException ex)@b@ {@b@ }@b@ }@b@ }@b@ }@b@@b@ public String determineDatabaseType(String driverName, String jdbcConnectionUrl)@b@ {@b@ if (this.jdbcDriverToPlatform.containsKey(driverName))@b@ {@b@ return ((String)this.jdbcDriverToPlatform.get(driverName));@b@ }@b@ if (jdbcConnectionUrl == null)@b@ {@b@ return null;@b@ }@b@ for (Iterator it = this.jdbcSubProtocolToPlatform.entrySet().iterator(); it.hasNext(); )@b@ {@b@ Map.Entry entry = (Map.Entry)it.next();@b@ String curSubProtocol = "jdbc:" + ((String)entry.getKey()) + ":";@b@@b@ if (jdbcConnectionUrl.startsWith(curSubProtocol))@b@ {@b@ return ((String)entry.getValue());@b@ }@b@ }@b@ return null;@b@ }@b@}