1. 程式人生 >實用技巧 >其它 >2021-05-19

2021-05-19


JDBC中資料庫的五個連線方式


前言

方式一、方式二、方式三、方式四:作為過程存在,瞭解即可,在實際運用中掌握方式五即可。


方式一

程式碼如下(示例):

import org.junit.Test;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;


@Test
    public void test1() throws SQLException {
    // 1.獲取Driver實現類物件 Driver是一個介面,com.Mysql.jdbc是它的實現類
            Driver driver = new com.mysql.jdbc.Driver();

            // url:http://localhost:8080/gmall/keyboard.jpg
            // jdbc:mysql:協議
            // localhost:ip地址
            // 3306:預設mysql的埠號
            // test:test資料庫
            String url = "jdbc:mysql://localhost:3306/test";
            // 將使用者名稱和密碼封裝在Properties中
            Properties info = new Properties();
            info.setProperty("user", "root");
            //mysql001的密碼
            info.setProperty("password", "1234");
            //多型
            Connection conn = driver.connect(url, info);

            System.out.println(conn);
            //資源的關閉
            conn.close();

    }

輸出結果:

2021-05-19


返回地址值說明連線成功

方式二

注:對test1的迭代:在如下程式中不出現第三方的api,使得程式具有更好的可移植性
程式碼如下(示例):

@Test
    public void test2() throws Exception {
        //1.獲取 Driver實現類物件,使用反射
        Class clazz = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) clazz.newInstance();
        //2.提供要連線的資料庫
        String url = "jdbc:mysql://localhost:3306/test";
        //3.提供連線需要的使用者名稱和密碼
        Properties info = new Properties();
        info.setProperty("user","root");
        info.setProperty("password","1234");
        //4.獲取連線
        Connection conn = driver.connect(url, info);
        System.out.println(conn);
        //5.資源的關閉
        conn.close();
    }

方式三

注:使用DriverManager代替Driver DriverManager是一個具體實現類
程式碼如下(示例):

@Test
    public void test3() throws Exception {
        //1.實現Driver實現類的物件
        Class clazz = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver)clazz.newInstance();
        //2.提供另外三個連線的基本資訊
        String url = "jdbc:mysql://localhost:3306/test";
        String user = "root";
        String password = "1234";
        //3.註冊驅動
        DriverManager.registerDriver(driver);
        //4.獲取連線
        Connection conn = DriverManager.getConnection(url,user,password);
        System.out.println(conn);
        //5.資源的關閉
        conn.close();
    }

方式四

注:在三的基礎上做一些優化;只是載入驅動,不用顯示的註冊驅動
程式碼如下(示例):

@Test
    public void test4() throws Exception {
        //1.提供另外三個連線的基本資訊
        String url = "jdbc:mysql://localhost:3306/test";
        String user = "root";
        String password = "1234";
        //2.載入Driver,載入時執行靜態程式碼塊
        Class clazz = Class.forName("com.mysql.jdbc.Driver");
        //3.獲取連線
        Connection conn = DriverManager.getConnection(url,user,password);
        System.out.println(conn);
        //5.資源的關閉
        conn.close();
        /*
        因為在Driver類裡有一個靜態程式碼塊
    static {
        try {
            DriverManager.registerDriver(new Driver());
        } catch (SQLException var1) {
            throw new RuntimeException("Can't register driver!");
        }
    }
        */
    }
    

方式五(最終版)

注:將資料庫連線需要的四個基本資訊宣告在配置檔案中,通過讀取配置檔案的方式獲取連線
程式碼如下(示例):

@Test
    public void teat5() throws Exception {
        //1.讀取配置檔案中的四個基本資訊
        //在src下建立file(jdbc.properties)
        InputStream is = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties pros = new Properties();
        pros.load(is);

        String user = pros.getProperty("user");
        String password = pros.getProperty("password");
        String url = pros.getProperty("url");
        String driverClass = pros.getProperty("driverClass");
        //2.載入驅動
        Class.forName(driverClass);
        //3.獲取連線
        Connection conn = DriverManager.getConnection(url,user,password);
        System.out.println(conn);
        //4.資源的關閉
        conn.close();

    }

其中,配置檔案【jdbc.properties】:此配置檔案宣告在工程的src下
user=root
password=abc123
url=jdbc:mysql://localhost:3306/test
rewriteBatchedStatements=true
driverClass=com.mysql.jdbc.Driver
注:在實際應用中不應該丟擲異常,而是要try-catch,這裡這樣寫是為了看清結構

執行結果:

2021-05-19

總結

方式五的好處:
①實現資料與程式碼的分離,實現瞭解耦;
②如果需要修改配置檔案資訊,可以避免程式重寫打包

第一次寫部落格,如果有什麼語法錯誤,邏輯不清楚,亦或是有什麼需要改進的地方,非常歡迎和我交流,希望大家多多指正!