JDBC--獲取數據庫鏈接

1、JDBC基礎

    JDBC(Java Database Connectivity)是一個獨立與特定數據庫管理系統、通用的SQL數據庫存取和操做的公共接口(一組API),定義了用來訪問數據庫的標準Java類庫。java

2、JDBC體系結構

JDBC接口(API)包括兩個層次:mysql

  • 面向應用的API:Java API,抽象接口,供應用程序開發人員使用(鏈接數據庫,執行SQL語句,得到結果)。
  • 面向數據庫的API:Java Driver API,供開發商開發數據庫驅動程序用。

JDBC API 是一系列的接口,它使得應用程序可以進行數據庫聯接,執行SQL語句,而且獲得返回結果。sql

3、JDBC獲取數據庫鏈接

1. Driver 接口數據庫

  • Java.sql.Driver 接口是全部 JDBC 驅動程序須要實現的接口。這個接口是提供給數據庫廠商使用的,不一樣數據庫廠商提供不一樣的實現
  • 在程序中不須要直接去訪問實現了 Driver 接口的類,而是由驅動程序管理器類(java.sql.DriverManager)去調用這些Driver實現

2. 創建鏈接oracle

    能夠調用 DriverManager 類的 getConnection() 方法創建到數據庫的鏈接函數

3. JDBC URLsqlserver

    JDBC URL 用於標識一個被註冊的驅動程序,驅動程序管理器經過這個 URL 選擇正確的驅動程序,從而創建到數據庫的鏈接。學習

4. 幾種經常使用數據庫的JDBC URLthis

格式:url

對於 Oracle 數據庫鏈接,採用以下形式:

  • jdbc:oracle:thin:@localhost:1521:sid

對於 SQLServer 數據庫鏈接,採用以下形式:

  • jdbc:microsoft:sqlserver//localhost:1433; DatabaseName=sid

對於 MYSQL 數據庫鏈接,採用以下形式:  

  • jdbc:mysql://localhost:3306/sid

(1)獲取數據庫鏈接--直接調用Driver接口

/**
     * Driver 是一個接口:數據庫廠商必需提供實現的接口
     * @throws SQLException
     */
    @Test
    public void test1() throws SQLException {
        //1.建立一個Driver類的對象
        Driver driver = new com.mysql.jdbc.Driver();

        //2.鏈接數據庫的基本信息:url、user、password
        String url = "jdbc:mysql://localhost:3306/jdbctest";
        Properties info = new Properties();
        info.put("user", "root");
        info.put("password", "12345");

        //3.調用Driver接口的connect獲取數據庫鏈接
        Connection connection = driver.connect(url, info);
        System.out.println(connection);
    }

方法改進----通用方法獲取數據庫鏈接

把數據庫驅動 Driver實現類的全類名、url、user、password放入一個配置文件中,經過修改配置文件的方式來實現數據庫鏈接。

/**
     * 寫一個方法,在不修改源程序的狀況下,能夠獲取任何數據庫的鏈接
     * 解決方案:把數據庫驅動 Driver實現類的全類名、url、user、password放入
     * 一個配置文件中,經過修改配置文件的方式來實現數據庫鏈接
     */
    public Connection getConnection() throws Exception {
        String driverClass = null;
        String jdbcUrl = null;
        String user = null;
        String password = null;

        //讀取類路徑下的 jdbc.properties 文件
        java.io.InputStream in = getClass().getClassLoader().getResourceAsStream("jdbc.properties");
        Properties properties = new Properties();
        properties.load(in);
        driverClass = properties.getProperty("driver");
        user = properties.getProperty("user");
        password = properties.getProperty("password");
        jdbcUrl = properties.getProperty("jdbcUrl");
        
        //經過反射建立Driver對象
        Driver driver = (Driver) Class.forName(driverClass).newInstance();
        Properties info = new Properties();
        info.put("user", user);
        info.put("password", password);
        Connection connection = driver.connect(jdbcUrl,info);

        return connection;
    }

    @Test
    public void test2() throws Exception {
        System.out.println(getConnection());
    }


//配置文件:jdbc.properties
//位於類路徑下
driver=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/jdbctest
user=root
password=12345

(2)DriverManager 管理驅動

/**
     * DriverManager 是驅動的管理類
     * 1.能夠經過重載的 getConnection方法獲取數據庫的鏈接
     * 2.能夠同時管理多個驅動程序:若註冊了多個數據庫鏈接,則調用getConnection()
     * 方法時傳入的參數不一樣,即返回不一樣的數據庫鏈接
     * @throws Exception
     */
    @Test
    public void test3() throws Exception {
        //1.準備鏈接數據庫的4個字符串
        //驅動的全類名
        String driverClass = "com.mysql.jdbc.Driver";
        //JDBC URL
        String jdbcUrl = "jdbc:mysql://localhost:3306/jdbctest";
        //用戶名
        String user = "root";
        //密碼
        String password = "620422@MS";


        //2.加載數據庫驅動程序(對應的Driver實現類中有註冊驅動的靜態代碼塊)
        //DriverManager.registerDriver((Driver) Class.forName(driverClass).newInstance());
        Class.forName(driverClass);

        //3.經過DriverManager的getConnection方法獲取數據庫鏈接
        Connection connection = DriverManager.getConnection(jdbcUrl, user, password);
        System.out.println(connection);
    }

方法改進----通用方法

public Connection getConnection2() throws Exception {
        //1.準備鏈接數據庫的4個字符串
        //1.1 建立Properties對象
        Properties properties = new Properties();
        //1.2 獲取jdbc.properties對應的輸入流
        java.io.InputStream in = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");
        //1.3 加載文件
        properties.load(in);
        //1.4 給字符串賦值
        String driver = properties.getProperty("driver");
        String jdbcUrl = properties.getProperty("jdbcUrl");
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        //2.加載數據庫驅動程序(對應的Driver實現類中有註冊驅動的靜態代碼塊)
        Class.forName(driver);
        //3.經過DriverManager的getConnection方法獲取數據庫鏈接
        return DriverManager.getConnection(jdbcUrl, user, password);
    }

    @Test
    public void test4() throws Exception {
        System.out.println(getConnection2());
    }

//配置文件:jdbc.properties
driver=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/jdbctest
user=root
password=12345

 

 

JDBC學習筆記:

1. 獲取數據庫鏈接    ----當前----

2. 經過Statement執行更新、查詢操做    http://my.oschina.net/daowuming/blog/704384

3. 使用PrepareStatement    http://my.oschina.net/daowuming/blog/704432

4. 使用ResultSetMetaData 對象處理結果集元數據    http://my.oschina.net/daowuming/blog/704487

5. 使用DatabaseMetaData獲取數據庫信息    http://my.oschina.net/daowuming/blog/704553

6. BLOB    http://my.oschina.net/daowuming/blog/704593

7. 處理事務與隔離級別    http://my.oschina.net/daowuming/blog/704611

8. 批量處理    http://my.oschina.net/daowuming/blog/704641

9. 數據庫鏈接池    http://my.oschina.net/daowuming/blog/704700

10. 調用函數與存儲過程    http://my.oschina.net/daowuming/blog/704813

相關文章
相關標籤/搜索