jdbc-1-入門

介紹

  • jdbc:java database connectivity,是java規範中描述客戶端如何訪問數據庫的接口
    • 注意:jdbc面向的關係型的
  • 包括:
    • java api:位於java.sql和javax.sql包中
    • database api:規範各個數據庫的接入標準

預備

  • 下載mysql workbench的mac版本
  • 數據庫url:
    • 示例1:Jdbc:mysql://localhost:3306/test?key1=param1&key2=param2
    • 示例2:jdbc:mysql://localhost:3306/test?user=root&password=&useUnicode=true&characterEncoding=gbk
Jdbc:連接協議
     mysql:連接子協議
     Localhost:3306:連接地址
     test:數據庫名稱
     &:存在編碼問題,用該編碼代替”&“
     ?號後面的都是連接時的參數,加上時,會覆蓋默認的連接參數
  • url參數文檔:
    • https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html
    • 常用的url參數有:
User:用戶名
password:用戶密碼
connectTimeout:socket連接超時時間
maxReconnects:重新連接時間
useSSL:是否使用useSSL,默認爲false
characterEncoding:編碼格式,一般爲utf-8
serverTimezone:設置MySQL的時區
  • mysql驅動
    • 注意:8.x版本的mysql驅動有非常大的變化,需要注意

連接案例

package connection;

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;

public class First {

    private static String url = "jdbc:mysql://localhost:3306/test";
    private static String user = "root";
    private static String password = "123456";

    /**
     * 方式1:直接聲明驅動和連接屬性
     */
    @Test
    public void testGetConnection01() throws SQLException {
        // 1. 聲明驅動
        Driver driver = new com.mysql.jdbc.Driver();
        // 2. 連接db,參數:
        //  url、
        //  Properties info:連接屬性,至少得包括user、password兩個key
        Properties info = new Properties();
        info.setProperty("user", user);
        info.setProperty("password", password);
        // 3. 開始連接
        Connection connect = driver.connect(url, info);
        System.out.println("new得到連接:" + connect);
    }

    /**
     * 方式2:使用反射機制生成
     */
    @Test
    public void testGetConnection02() throws Exception {
        Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver)aClass.newInstance();
        Properties info = new Properties();
        info.setProperty("user", user);
        info.setProperty("password", password);
        Connection connect = driver.connect(url, info);
        System.out.println("反射得到連接:" + connect);
    }

    /**
     * 方式3:使用DriverManager-詳細版
     */
    @Test
    public void testGetConnection03() throws Exception {
        Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver)aClass.newInstance();
        // 註冊Driver
        DriverManager.registerDriver(driver);

        Connection connection = DriverManager.getConnection(url, user, password);

        System.out.println("DriverManager得到連接:" + connection);
    }
    /**
     * 方式4:使用DriverManager-精簡版
     *  但實際上,使用過程中,最好還是聲明一下具體的驅動類,因爲非mysql的可能並不一定是這麼實現的
     */
    @Test
    public void testGetConnection04() throws Exception {
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println("簡化版DriverManager得到連接:" + connection);
        /**
         * 因爲mysql的Driver有個靜態代碼塊,在類加載的時候,就已經註冊了DriverManager:
         *     static {
         *         try {
         *             java.sql.DriverManager.registerDriver(new Driver());
         *         } catch (SQLException E) {
         *             throw new RuntimeException("Can't register driver!");
         *         }
         *     }
         */
    }

    /**
     * 方式5:使用DriverManager+配置方式
     *  1. 配置與代碼分離,實現解耦
     *  2. 
     */
    @Test
    public void testGetConnection05() throws Exception {
        InputStream resourceAsStream = First.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties properties = new Properties();
        properties.load(resourceAsStream);

        Class<?> driverClass = Class.forName(properties.getProperty("driverClass"));
        Connection connection = DriverManager.getConnection(properties.getProperty("url"),
                properties.getProperty("user"),
                properties.getProperty("password"));
        System.out.println("配置文件版的DriverManager得到連接:" + connection);
    }
}

  • 輸出:
    在這裏插入圖片描述

參考

  • 尚硅谷jdbc教程
  • 延伸:
    • 以前的SSH框架:structs、spring、Hibernate
    • 現在的SSM框架:spring、springmvc、mabatis
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章