1. 程式人生 > 其它 >獲取資料庫連線的方式

獲取資料庫連線的方式

要連線資料庫,需要有driver(驅動)、url(資料庫地址)、username(使用者名稱)、password(密碼)。

package com.hllog;

import com.mysql.cj.jdbc.Driver;
import org.junit.Test;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class ConnectionTest {
    @Test
    public void testConnection1() throws SQLException {
        Driver driver = new Driver();
        String url = "jdbc:mysql://localhost:3306/test2";
        Properties info = new Properties();
        info.setProperty("user", "root");
        info.setProperty("password", "xxxxxx");

        Connection connection = driver.connect(url, info);
        System.out.println(connection);
    }

    // 改進:不出現第三方API,使程式具有更好的可移植性
    @Test
    public void testConnection2() throws Exception {
        Class clazz = Class.forName("com.mysql.cj.jdbc.Driver");
        Driver driver = (Driver) clazz.newInstance();
        String url = "jdbc:mysql://localhost:3306/test2";
        Properties info = new Properties();
        info.setProperty("user", "root");
        info.setProperty("password", "xxxxxx");

        Connection connection = driver.connect(url, info);
        System.out.println(connection);
    }

    // 使用DriverManager替換Driver
    @Test
    public void testConnection3() throws Exception {
        Class clazz = Class.forName("com.mysql.cj.jdbc.Driver");
        Driver driver = (Driver) clazz.newInstance();
        String url = "jdbc:mysql://localhost:3306/test2";
        String user="root";
        String password = "xxxxxx";
        DriverManager.registerDriver(driver);
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println(connection);
    }

    // 自動註冊Driver,Driver的靜態程式碼塊完成
    @Test
    public void testConnection4() throws Exception {
        String url = "jdbc:mysql://localhost:3306/test2";
        String user="root";
        String password = "xxxxxx";
        Class clazz = Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println(connection);
    }

    // 常用
    @Test
    public void testConnection5() throws Exception {
        InputStream is = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties properties = new Properties();
        properties.load(is);
        String driver = properties.getProperty("driver");
        String url = properties.getProperty("url");
        String username = properties.getProperty("username");
        String password = properties.getProperty("password");
        Class clazz = Class.forName(driver);
        Connection connection = DriverManager.getConnection(url, username, password);
        System.out.println(connection);
    }
}

jdbc.properties檔案

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/test2
username=root
password=xxxxxx

以上內容學自尚矽谷的宋紅康老師的JDBC課程。