JDBC加載數據庫驅動的方式

  JDBC做爲數據庫訪問的規範接口,其中只是定義一些接口。具體的實現是由各個數據庫廠商來完成。java

  1、重要的接口:mysql

  1.public interface Driver 每一個驅動程序類必須實現的接口。Java SQL 框架容許多個數據庫驅動程序。每一個驅動程序都應該提供一個實現 Driver 接口的類。DriverManager 會試着加載儘量多的它能夠找到的驅動程序,而後,對於任何給定鏈接請求,它會讓每一個驅動程序依次試着鏈接到目標 URL。強烈建議每一個 Driver 類應該是小型的而且是單獨的,這樣就能夠在沒必要引入大量支持代碼的狀況下加載和查詢 Driver 類。在加載某一 Driver 類時,它應該建立本身的實例並向 DriverManager 註冊該實例。這意味着用戶能夠經過調用如下程序加載和註冊一個驅動程序   Class.forName("foo.bah.Driver")。例如:MYSQL驅動 com.mysql.jdbc.Driversql

  2.public interface Connection 與特定數據庫的鏈接(會話)。在鏈接上下文中執行 SQL 語句並返回結果。數據庫

  3.public interface Statement 用於執行靜態 SQL 語句並返回它所生成結果的對象。框架

  4.public interface PreparedStatement 表示預編譯的 SQL 語句的對象。SQL 語句被預編譯並存儲在 PreparedStatement 對象中。而後可使用此對象屢次高效地執行該語句。spa

  2、驅動的加載方式code

  1.最經常使用的是使用 Class.forName("com.mysql.jdbc.Driver");方式。這行代碼只是使用當前的類加載去加載具體的數據庫驅動,不要小看這簡單的這一行代碼。在Driver類中的static域中把當前驅動註冊到DriverManager中。對象

 static {
        try {
            java.sql.DriverManager.registerDriver(new Driver());//註冊驅動
        } catch (SQLException E) {
            throw new RuntimeException("Can't register driver!");
        }
    }

  2.經過查看DriverManager源碼,咱們也可使用System.setProperty("jdbc.drivers","....")方式。blog

String drivers;
try {
drivers = AccessController.doPrivileged(new PrivilegedAction<String>() {
public String run() {
return System.getProperty("jdbc.drivers");
}
});
} catch (Exception ex) {
drivers = null;
}
String[] driversList = drivers.split(":");
println("number of Drivers:" + driversList.length);
for (String aDriver : driversList) {
try {
println("DriverManager.Initialize: loading " + aDriver);
Class.forName(aDriver, true,
ClassLoader.getSystemClassLoader());
} catch (Exception ex) {
println("DriverManager.Initialize: load failed: " + ex);
}
}

  3.最直接(不推薦)方式new com.mysql.jdbc.Driver();接口

  4.爲了更好的使用數據庫驅動,JDBC爲咱們提供了DriverManager類。若是咱們都沒有使用以上方式,DriverManager初始化中會經過ServiceLoader類,在咱們classpath中jar(數據庫驅動包)中查找,如存在META-INF/services/java.sql.Driver文件,則加載該文件中的驅動類。

 AccessController.doPrivileged(new PrivilegedAction<Void>() {
            public Void run() {

                ServiceLoader<Driver> loadedDrivers = ServiceLoader.load(Driver.class);
                Iterator<Driver> driversIterator = loadedDrivers.iterator();

                /* Load these drivers, so that they can be instantiated.
                 * It may be the case that the driver class may not be there
                 * i.e. there may be a packaged driver with the service class
                 * as implementation of java.sql.Driver but the actual class
                 * may be missing. In that case a java.util.ServiceConfigurationError
                 * will be thrown at runtime by the VM trying to locate
                 * and load the service.
                 *
                 * Adding a try catch block to catch those runtime errors
                 * if driver not available in classpath but it's
                 * packaged as service and that service is there in classpath.
                 */
                try{
                    while(driversIterator.hasNext()) {
                        driversIterator.next();
                    }
                } catch(Throwable t) {
                // Do nothing
                }
                return null;
            }
        });
相關文章
相關標籤/搜索