A simple class to automatically load the correct (most popular) JDBC driver base on the connection URL. If you have other driver URL and driver that I missed here, please send it via comments or email me and I will update this class.
package org.cnci.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashMap;
import java.util.Map;
/*
* ConnectionManager
* @author: T.A. Nguyen
*
* Simple solution to automatically load the JDBC driver based on the
* connection url. You can arrange the order and move the most used
* to the top to provide sequential search.
*/
public class ConnectionManager {
private static final Map DRVS = new HashMap();
static {
DRVS.put("jdbc:oracle:thin", "oracle.jdbc.OracleDriver");
DRVS.put("jdbc:sqlserver:", "com.microsoft.sqlserver.jdbc.SQLServerDriver");
DRVS.put("jdbc:weblogic:sqlserver:", "weblogic.jdbc.sqlserver.SQLServerDriver");
DRVS.put("jdbc:microsoft:sqlserver:", "com.microsoft.jdbc.sqlserver.SQLServerDriver");
DRVS.put("jdbc:db2:", "com.ibm.db2.jcc.DB2Driver");
DRVS.put("jdbc:informix-sqli:", "com.informix.jdbc.IfxDriver");
DRVS.put("jdbc:mysql:", "com.mysql.jdbc.Driver");
DRVS.put("jdbc:netezza:", "org.netezza.Driver");
DRVS.put("jdbc:oracle:oci", "oracle.jdbc.OracleDriver");
DRVS.put("jdbc:sybase:Tds:", "com.sybase.jdbc4.jdbc.SybDriver");
DRVS.put("jdbc:teradata:", "com.ncr.teradata.TeraDriver");
DRVS.put("jdbc:jtds:", "net.sourceforge.jtds.jdbc.Driver");
DRVS.put("jdbc:postgresql:", "org.postgresql.Driver");
DRVS.put("jdbc:Cache:", "com.intersys.jdbc.CacheDriver");
DRVS.put("jdbc:FrontBase:", "com.frontbase.jdbc.FBJDriver");
DRVS.put("jdbc:h2:", "org.h2.Driver");
DRVS.put("jdbc:hsqldb:hsql:", "org.hsqldb.jdbcDriver");
//DRVS.put("jdbc:derby:", "org.apache.derby.jdbc.ClientDriver"); // Automatically loaded by JVM
DRVS.put("jdbc:mimer:", "com.mimer.jdbc.Driver");
DRVS.put("jdbc:pervasive:", "com.pervasive.jdbc.v2.Driver");
DRVS.put("jdbc:sqlite:", "org.sqlite.JDBC");
DRVS.put("jdbc:as400:", "com.ibm.as400.access.AS400JDBCDriver");
DRVS.put("jdbc:mckoi:", "com.mckoi.JDBCDriver");
DRVS.put("jdbc:firebirdsql:", "org.firebirdsql.jdbc.FBDriver");
DRVS.put("jdbc:rmi:", "RmiJdbc.RJDriver");
DRVS.put("jdbc:ids", "ids.sql.IDSDriver");
DRVS.put("jdbc:idb:", "org.enhydra.instantdb.jdbc.idbDriver"); // InstantDB
DRVS.put("jdbc:interbase:", "interbase.interclient.Driver");
DRVS.put("jdbc:HypersonicSQL:", "org.hsql.jdbcDriver");
DRVS.put("jdbc:weblogic:mssqlserver4:", "weblogic.jdbc.mssqlserver4.Driver");
DRVS.put("jdbc:pointbase:", "com.pointbase.jdbc.jdbcUniversalDriver");
DRVS.put("jdbc:cloudscape:", "COM.cloudscape.core.JDBCDriver");
DRVS.put("jdbc:JTurbo:", "com.ashna.jturbo.driver.Driver");
DRVS.put("jdbc:inetdae:", "com.inet.tds.TdsDriver");
DRVS.put("jdbc:cognos:", "cs.jdbc.driver.CompositeDriver");
DRVS.put("jdbc:neon:", "com.neon.jdbc.Driver");
}
private ConnectionManager() {
}
public static String getDriver(String url) {
if (url != null) {
for (String prefix : DRVS.keySet()) {
if (url.startsWith(prefix)) {
return DRVS.get(prefix);
}
}
}
return null;
}
public static Connection getConnection(String url, String user, String password) throws Exception {
Connection conn = null;
if (url != null) {
Class.forName(getDriver(url));
if (user != null && password != null) {
conn = DriverManager.getConnection(url, user, password);
} else {
conn = DriverManager.getConnection(url);
}
}
return conn;
}
}