获取数据库连接的五种方式


package com.qf.JDBC.four;

import org.testng.annotations.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 test {
//第一种方式
    @Test
    public void test1 () throws SQLException{
        //加载驱动
    Driver driver1=new com.mysql.jdbc.Driver();
    //提供要连接的数据库
    String url1="jdbc:mysql://localhost:3306/companydb";
    //将用户名和密码封装在Properties中
    Properties info1 =new Properties();
    info1.setProperty("user","root");
    info1.setProperty("password","1234");
        //4.获取连接
    Connection connection1=driver1.connect(url1,info1);
        System.out.println(connection1);
    }
    //方式二:对方式一的迭代:在如下的程序中不出现第三方的api,使得程序具有更好的可移植性。
    @Test
    public void test2() throws Exception {
        //1.获取Driver实现类对象:使用反射
        Class clazz2 = Class.forName("com.mysql.jdbc.Driver");
        Driver driver2 = (Driver) clazz2.newInstance();
        //2.提供要连接的数据库
        String url = "jdbc:mysql://localhost:3306/companydb";
        //3.提供连接需要的用户名和密码

        Properties info2 = new Properties();
        info2.setProperty("user", "root");
        info2.setProperty("password", "1234");
        //4.获取连接
        Connection connection2 = driver2.connect(url, info2);
        System.out.println(connection2);

    }
        //方式三:使用DriverManager替换Driver
        @Test
        public void test3 () throws Exception {
            //1.获取Driver的实现类对象
            Class clazz3 = Class.forName("com.mysql.jdbc.Driver");
            Driver driver3 = (Driver) clazz3.newInstance();
            //2.提供另外三个连接的基本信息
            String url3 = "jdbc:mysql://localhost:3306/companydb";
            String user3 = "root";
            String password3 = "1234";
            //3.注册驱动
            DriverManager.registerDriver(driver3);
            //4.获取连接
            Connection connection3 = DriverManager.getConnection(url3, user3, password3);
            System.out.println(connection3);
        }


    //方式四:可以只是加载驱动,不用显示的注册驱动过了
    @Test
    public void test4 () throws Exception {

        //1.提供另外三个连接的基本信息
        String url4= "jdbc:mysql://localhost:3306/companydb";
        String user4= "root";
        String password4 = "1234";

        //2.加载驱动
      /*  Class clazz4 =*/ Class.forName("com.mysql.jdbc.Driver");//com.mysql.jdbc.Driver就直接加载驱动了
        /*
    static {
        try {
            DriverManager.registerDriver(new Driver());
        } catch (SQLException var1) {
            throw new RuntimeException("Can't register driver!");
        }
    }静态代码块,随着类的加载而执行
         */

//        Driver driver4 = (Driver) clazz4.newInstance();
//
//        //3.注册驱动
//        DriverManager.registerDriver(driver4);
        //4.获取连接
        Connection connection4 = DriverManager.getConnection(url4, user4, password4);
        System.out.println(connection4);
    }

    //方式五(final):将数据库连接需要的4个基本信息声明在配置文件中,通过读取配置文件的 方式,获取连接
    //好处:1.实现了代码与数据的分离,实现了解耦
    //2.如果需要修改配置文件,可以避免程序重新打包
    @Test
    public void test5() throws Exception {

        //1.读取配置文件中的4个基本信息
        InputStream is = test.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);

    }

}