JDBC之获取数据库连接

Driver接口实现类

java.sql.Driver 接口是所有 JDBC 驱动程序需要实现的接口。这个接口是提供给数据库厂商使用的,不同数据库厂商提供不同的实现。

加载和注册JDBC驱动

加载驱动:

  • 加载 JDBC 驱动需调用 Class 类的静态方法 forName(),向其传递要加载的 JDBC 驱动的类名,Class.forName(“com.mysql.jdbc.Driver”)

注册驱动:

  • DriverManager 类是驱动程序管理器类,负责管理驱动程序使用DriverManager.registerDriver(com.mysql.jdbc.Driver)来注册驱动通常不用显式调用 DriverManager 类的 registerDriver() 方法来注册驱动程序类的实例,因为 Driver 接口的驱动程序类都包含了静态代码块,在这个静态代码块中,会调用 DriverManager.registerDriver() 方法来注册自身的一个实例

以下是源码:

 

方式1

package com.connection;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @author 承夕
 * @date 2020/2/25 0025 - 17:15
 * @contact:https://github.com/chengxi0
 */
public class ConnectionDeno1 {
    public static void main(String[] args) {
        try {
            //提供java.sql.Driver接口实现的类
            Driver driver = null ;
            driver = new com.mysql.jdbc.Driver();

            //提供url,指明具体操作数据
            String url = "jdbc:mysql://localhost:3306/test";

            //提供Properties对象,指明用户名和密码
            Properties properties = new Properties();
            properties.setProperty("user", "root");
            properties.setProperty("password", "923403");

            Connection connect = driver.connect(url, properties);
            System.out.println(connect);

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

方式2

package com.connection;

import com.mysql.jdbc.Driver;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @author 承夕
 * @date 2020/2/25 0025 - 23:35
 * @contact:https://github.com/chengxi0
 */
public class ConnectionDemo2 {
    public static void main(String[] args) {
        try {
        //数据库连接的4个基本要素
        String url = "jdbc:mysql://localhost:3306/test";
        String user = "root";
        String password = "922303";
        String driverName = "com.mysql.jdbc.Driver";

        //实例化Driver
        Class<?> clazz = Class.forName(driverName);
        Driver driver =(Driver) clazz.newInstance();

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

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

            System.out.println(connection);
        } catch (ClassNotFoundException | SQLException | InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }


    }
}

方式3

package com.connection;

import com.mysql.jdbc.Driver;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @author 承夕
 * @date 2020/2/25 0025 - 23:35
 * @contact:https://github.com/chengxi0
 */
public class ConnectionDemo3 {
    public static void main(String[] args) {
        try {
        //数据库连接的4个基本要素
        String url = "jdbc:mysql://localhost:3306/test";
        String user = "root";
        String password = "922303";
        String driverName = "com.mysql.jdbc.Driver";

        //实例化Driver
            Class.forName(driverName);

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

            System.out.println(connection);
        } catch (ClassNotFoundException | SQLException  e) {
            e.printStackTrace();
        }
    }
}

这种方式省去了显式的注册驱动,因为在开始的时候说了加载Driver这个类的时候,有个静态代码块,里面就已经注册驱动了。

方式4(最终方式)

package com.connection;

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

/**
 * @author 承夕
 * @date 2020/2/25 0025 - 23:35
 * @contact:https://github.com/chengxi0
 */
public class ConnectionDemo4 {
    public static void main(String[] args) {
        try {
            //加载配置文件
        InputStream inputStream = ConnectionDemo4.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties properties = new Properties();
        properties.load(inputStream);

        //读取配置信息
            String  user = (String)properties.get("user");
            System.out.println(user);
            String  password = (String)properties.get("password");
            System.out.println(password);
            String  url = (String)properties.get("url");
            System.out.println(url);
            String  driverClass = (String)properties.get("driverClass");
            System.out.println(driverClass);

            //加载注册驱动
            Class.forName(driverClass);

            //获取连接
            Connection connection = DriverManager.getConnection(url, user, password);
            System.out.println(connection);
        } catch (IOException | SQLException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

其中的properties文件如下

user=root
password=922303
url=jdbc:mysql://localhost:3306/test
driverClass=com.mysql.jdbc.Driver

说明:使用配置文件的方式保存配置信息,在代码中加载配置文件

使用配置文件的好处:

  • ①实现了代码和数据的分离,如果需要修改配置信息,直接在配置文件中修改,不需要深入代码,
  • ②如果修改了,配置信息,省去重新编译的过程。

其实这种方式中利用properties对象的load方法加载输入流,和方式1 利用properties对象setProperties方法本质上是一样的。

 

 

 

发布了55 篇原创文章 · 获赞 4 · 访问量 1056

猜你喜欢

转载自blog.csdn.net/weixin_45062761/article/details/104504000