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;

public class Main {
    //  方式1
    @Test
    public void testConnection1() throws SQLException {
        Driver driver = new com.mysql.cj.jdbc.Driver();

        String url = "jdbc:mysql://114.115.162.49:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT";
        Properties info = new Properties();
        info.setProperty("user", "root");
        info.setProperty("password", "tiger");
        Connection conn = driver.connect(url, info);
        System.out.println(conn);
    }

    //  方式2 利用反射来创建连接,未出现第三方api
    @Test
    public void testConnection2() throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException {
        Class clazz = Class.forName("com.mysql.cj.jdbc.Driver");
        Driver driver = (Driver) clazz.newInstance();
        String url = "jdbc:mysql://114.115.162.49:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT";
        Properties info = new Properties();
        info.setProperty("user", "root");
        info.setProperty("password", "tiger");
        Connection conn = driver.connect(url, info);
        System.out.println(conn);
    }

    // 方式3 利用DriverManager
    @Test
    public void testConnection3() throws Exception {
        Class clazz = Class.forName("com.mysql.cj.jdbc.Driver");
        Driver driver = (Driver) clazz.newInstance();

        String url = "jdbc:mysql://114.115.162.49:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT";
        DriverManager.deregisterDriver(driver);
        Connection conn = DriverManager.getConnection(url, "root", "tiger");
        System.out.println(conn);
    }

    // 方式4
    @Test
    public void testConnection4() throws Exception {
        Class.forName("com.mysql.cj.jdbc.Driver");
        String url = "jdbc:mysql://114.115.162.49:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT";
        Connection conn = DriverManager.getConnection(url, "root", "tiger");
        System.out.println(conn);

    }

    // 方式5 配置文件获取连接
   //  数据和代码分离
    @Test
    public void testConnection5() throws Exception {
        InputStream is =  Main.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties properties = new Properties();
        properties.load(is);
        String user= properties.getProperty("user");
        String password= properties.getProperty("password");
        String url= properties.getProperty("driverClass");
        String driverClass= properties.getProperty("driverClass");

        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection conn = DriverManager.getConnection(url, user, password);
        System.out.println(conn);
    }

}

posted @ 2022-03-17 20:49  诗酒  阅读(70)  评论(0编辑  收藏  举报