方式一 通过driver连接

@Test
public void test01() throws SQLException, IOException {
    //获取Driver实现类对象 第三方api
    Driver driver = new com.mysql.jdbc.Driver();

    //jdbc:mysql:-->协议
    //localhost:-->ip地址
    //3306-->默认端口号
    //test-->test数据库
    String url = "jdbc:mysql://localhost:3306/test";

    //封装用户密码
    Properties info = new Properties();
    info.setProperty("user", "root");
    info.setProperty("password", "123456");

    //获取连接
    Connection connection = driver.connect(url, info);

    //连接状态 com.mysql.jdbc.JDBC4Connection@fad74ee
    System.out.println(connection);
}

方式二 通过反射连接

对方式一的迭代-->在程序中不出现第三方的api,使得程序具有更好的可移植性

@Test
public void test02() throws Exception {
    //获取Driver实现类对象 使用反射
    Class<?> clazz = Class.forName("com.mysql.jdbc.Driver");
    Driver driver = (Driver) clazz.newInstance();

    //协议
    String url = "jdbc:mysql://localhost:3306/test";

    //封装用户密码
    Properties info = new Properties();
    info.setProperty("user", "root");
    info.setProperty("password", "123456");

    //获取连接
    Connection connection = driver.connect(url, info);

    //连接状态 com.mysql.jdbc.JDBC4Connection@fad74ee
    System.out.println(connection);

方式三 通过DriverManager连接

使用DriverManager替换Driver

@Test
public void test03() throws Exception {
    //协议
    String url = "jdbc:mysql://localhost:3306/test";

    //用户账号
    String user = "root";

    //用户密码
    String password = "123456";

    //获取Driver实现类对象 使用反射
    Class<?> clazz = Class.forName("com.mysql.jdbc.Driver");
    Driver driver = (Driver) clazz.newInstance();

    //注册驱动
    DriverManager.registerDriver(driver);

    //获取连接
    Connection connection = DriverManager.getConnection(url, user, password);

    //连接状态 com.mysql.jdbc.JDBC4Connection@fad74ee
    System.out.println(connection);
}

方式四 优化DriverManager连接

只加载驱动

@Test
public void test04() throws Exception {
    //协议
    String url = "jdbc:mysql://localhost:3306/test";

    //用户账号
    String user = "root";

    //用户密码
    String password = "123456";

    //加载驱动
    Class.forName("com.mysql.jdbc.Driver");
    /*在mysql的Driver实现类中,声明了以下操作
        static {
            try {
                DriverManager.registerDriver(new Driver());
            } catch (SQLException var1) {
                throw new RuntimeException("Can't register driver!");
            }
        }
     */


    //获取连接
    Connection connection = DriverManager.getConnection(url, user, password);

    //连接状态 com.mysql.jdbc.JDBC4Connection@fad74ee
    System.out.println(connection);
}

方式五 通过配置文件方式连接

优点:

1.实现了数据和代码的分离。实现了解耦

2.如果需要修改配置文件信息,可以避免重新打包

@Test
public void test05() throws Exception {
    //新建properties对象
    Properties properties = new Properties();
    //加载路径
    properties.load(new FileReader("src\\mysql.properties"));

    //获取user、password、url、driver
    String user = properties.getProperty("user");
    String password = properties.getProperty("password");
    String url = properties.getProperty("url");
    String driver = properties.getProperty("driver");

    //加载驱动
    Class.forName(driver);

    //获取连接
    Connection connection = DriverManager.getConnection(url, user, password);

    //连接状态 com.mysql.jdbc.JDBC4Connection@fad74ee
    System.out.println(connection);
}

Properties所在路径及内容(等号两边不能有空格,以免产生歧义) image.png