JDBC中獲取Connection的兩種方式

JDBC全稱Java Database Connectivity,是Java官方定義的一套連接數(shù)據(jù)庫(kù)的規(guī)范,里面包括各種API,其中最重要的就是關(guān)于如何獲取數(shù)據(jù)庫(kù)連接Connection的方法:

  • 直接調(diào)用DriverManager類的getConnection方法
  • 實(shí)現(xiàn)DataSource接口,調(diào)用實(shí)例的getConnection方法

DriverManager

初學(xué)JDBC的時(shí)候,每個(gè)同學(xué)首先都會(huì)接觸以下的代碼,步驟很簡(jiǎn)單,就是先注冊(cè)相應(yīng)數(shù)據(jù)庫(kù)的JDBC Driver,然后通過(guò)JDBC DriverManager獲取數(shù)據(jù)庫(kù)連接,之后從連接中執(zhí)行SQ語(yǔ)句。

Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(DB_URL,USER,PASS);
Statement stmt = conn.createStatement();
String sql;
sql = "SELECT id, first, last, age FROM Employees";
ResultSet rs = stmt.executeQuery(sql);
...

JDBC規(guī)范規(guī)定Driver類在加載時(shí)必須向DriverManager類注冊(cè)自己的實(shí)例,例如MySQL實(shí)現(xiàn)的Driver類在類加載的時(shí)候執(zhí)行static代碼塊,將Driver實(shí)例注冊(cè)在DriverManager類中,DriverManager類會(huì)維護(hù)一個(gè)CopyOnWriteArrayList來(lái)保存所有被注冊(cè)的Driver。


package com.mysql.jdbc;

import java.sql.SQLException;

public class Driver extends NonRegisteringDriver implements java.sql.Driver {
    //
    // Register ourselves with the DriverManager
    //
    static {
        try {
            java.sql.DriverManager.registerDriver(new Driver());
        } catch (SQLException E) {
            throw new RuntimeException("Can't register driver!");
        }
    }

    public Driver() throws SQLException {
        // Required for Class.forName().newInstance()
    }
}

java.sql.Driver是個(gè)接口類,其中最重要的是connect(String url, Properties info)方法,應(yīng)用程序在調(diào)用DriverManager的getConnection方法時(shí),內(nèi)部會(huì)遍歷所有的Driver來(lái)使用傳遞的URL去嘗試連接數(shù)據(jù)庫(kù),直到連接成功然后返回Connection實(shí)例。所以,Driver是獲取數(shù)據(jù)庫(kù)連接真正的類,每個(gè)數(shù)據(jù)庫(kù)廠商都在Driver.connect(String url, Properties info)里面封裝了自己的內(nèi)部實(shí)現(xiàn)邏輯,比如MySQL在connect方法內(nèi)部實(shí)現(xiàn)MySQL協(xié)議來(lái)和遠(yuǎn)程MySQL服務(wù)端建立TCP連接。

DataSource

JDBC規(guī)范提供了一個(gè)更加靈活的獲取Connection的方式,即DataSource接口,DataSource接口規(guī)定了必須實(shí)現(xiàn)getConnection等方法。由于這種方式比DriverManager更加抽象,用戶可以自己實(shí)現(xiàn)獲取Connection的方式,在應(yīng)用層也無(wú)需知道連接究竟是如何獲取的,所以在實(shí)際應(yīng)用中,人們往往基于DataSource來(lái)實(shí)現(xiàn)連接池、分庫(kù)分表中間件的功能。

DataSource : This interface is preferred over DriverManager
because it allows details about the underlying data source to be transparent to your application. A DataSource
object's properties are set so that it represents a particular data source. See Connecting with DataSource Objects for more information. For more information about developing applications with the DataSource
class, see the latest The Java EE Tutorial.

雖然有了DataSource,但是數(shù)據(jù)庫(kù)廠商依然要提供上述的Driver,因?yàn)橥ㄟ^(guò)Driver連接數(shù)據(jù)庫(kù)的方式是物理層連接,必不可少,所有的DataSource最底層建立物理連接的方式還是通過(guò)Driver類來(lái)操作,比如MysqlDataSource底層的連接、知名數(shù)據(jù)庫(kù)連接池Druid的DruidAbstractDataSource的createPhysicalConnection方法。

//MysqlDataSource.java
mysqlDriver.connect(jdbcUrlToUse, props);
// DruidAbstractDataSource.java
public Connection createPhysicalConnection(String url, Properties info) throws SQLException {
    Connection conn;
    if (getProxyFilters().size() == 0) {
        conn = getDriver().connect(url, info);
    } else {
        conn = new FilterChainImpl(this).connection_connect(info);
    }

    createCount.incrementAndGet();

    return conn;
}

參考資料

https://docs.oracle.com/javase/tutorial/jdbc/basics/index.html
https://docs.oracle.com/javase/7/docs/api/index.html?java/sql/DriverManager.html
https://dev.mysql.com/doc/connector-j/6.0/en/connector-j-usagenotes-basic.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • JDBC概述 在Java中,數(shù)據(jù)庫(kù)存取技術(shù)可分為如下幾類:JDBC直接訪問(wèn)數(shù)據(jù)庫(kù)、JDO技術(shù)、第三方O/R工具,如...
    usopp閱讀 3,643評(píng)論 3 75
  • 本人的環(huán)境為Myeclipse10、MySQL5.7.15 本文包括:簡(jiǎn)介JDBC編程步驟打通數(shù)據(jù)庫(kù)程序詳解—Dr...
    廖少少閱讀 4,365評(píng)論 7 39
  • JDBC簡(jiǎn)介 SUN公司為了簡(jiǎn)化、統(tǒng)一對(duì)數(shù)據(jù)庫(kù)的操作,定義了一套Java操作數(shù)據(jù)庫(kù)的規(guī)范,稱之為JDBC。JDBC...
    奮斗的老王閱讀 1,649評(píng)論 0 51
  • Connection對(duì)象表示通過(guò)啟用JDBC技術(shù)的驅(qū)動(dòng)程序與數(shù)據(jù)源的連接。 數(shù)據(jù)源可以是DBMS,傳統(tǒng)文件系統(tǒng)或其...
    zlb閱讀 1,672評(píng)論 0 1
  • Spark SQL, DataFrames and Datasets Guide Overview SQL Dat...
    草里有只羊閱讀 18,566評(píng)論 0 85

友情鏈接更多精彩內(nèi)容