java代码中加载MySQL Connector/J 驱动器

通过Class.forName(“com.mysql.jdbc.Driver”)加载:现在不推荐使用

例如:

package com.thb;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Enumeration;

public class JDBCDemo {

    public static void main(String[] args) throws ClassNotFoundException {       

。。。省略

        Class.forName("com.mysql.jdbc.Driver");
        Enumeration drivers = DriverManager.getDrivers();
        if (drivers.hasMoreElements()) {
            System.out.println("加载的驱动器的类型: "+ drivers.nextElement().getClass().getName());
        }
。。。省略        

}

运行输出片段:

Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
加载的驱动器的类型:com.mysql.cj.jdbc.Driver

通过Class.forName(“com.mysql.cj.jdbc.Driver”)加载:可以使用,但其实不需要显式加载

例如,下面代码中使用Class.forName(“com.mysql.cj.jdbc.Driver”)加载,注意,加载类的名称是com.mysql.cj.jdbc.Driver,而不是com.mysql.jdbc.Driver:

package com.thb;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Enumeration;

public class JDBCDemo {

    public static void main(String[] args) throws ClassNotFoundException {       

。。。省略

        Class.forName("com.mysql.cj.jdbc.Driver");
        Enumeration drivers = DriverManager.getDrivers();
        if (drivers.hasMoreElements()) {
            System.out.println("加载的驱动器的类型: "+ drivers.nextElement().getClass().getName());
        }
。。。省略        

}

运行输出片段:

加载的驱动器的类型: com.mysql.cj.jdbc.Driver

不显式加载

MySQL Connector/J 驱动器的类com.mysql.cj.jdbc.Driver是通过SPI 机制自动注册的,不需要显式加载。

例如,下面完整的示例代码,没有使用Class.forName来显式加载驱动器:

package com.thb;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Enumeration;

public class JDBCDemo {

    public static void main(String[] args) throws ClassNotFoundException {       
        String url = "jdbc:mysql://localhost:3306/development";
        String user = "test";
        String pass = "test000N*";
        Connection con = null;
        PreparedStatement stmt = null;
        String sql = "SELECT * FROM test_table";
        Enumeration drivers = DriverManager.getDrivers();
        if (drivers.hasMoreElements()) {
            System.out.println("加载的驱动器的类型: "+ drivers.nextElement().getClass().getName());
        }
        
        try {
            con = DriverManager.getConnection(url, user, pass);
            stmt = con.prepareStatement(sql);
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                System.out.println(rs.getInt("id"));
                System.out.println(rs.getString("type"));
                System.out.println(rs.getDate("pdate"));
                System.out.println(rs.getBigDecimal("price"));
                System.out.println(rs.getString("description"));                
            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
                if (con != null) {
                    con.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}

运行输出:

加载的驱动器的类型: com.mysql.cj.jdbc.Driver
1
1
2023-12-04
100.000000
测试数据

备注:com.mysql.jdbc.Driver是为了后向兼容而存在

看看MySQL Connector/J 中的 com.mysql.jdbc.Driver类的实现代码。它其实就为了后向兼容的目的,没有做什么实质工作,就是在构造函数中调用了com.mysql.cj.jdbc.Driver的构造函数:

package com.mysql.jdbc;

import java.sql.SQLException;

/**
 * Backwards compatibility to support apps that call Class.forName("com.mysql.jdbc.Driver");.
 */
public class Driver extends com.mysql.cj.jdbc.Driver {

    public Driver() throws SQLException {
        super();
    }

    static {
        System.err.println("Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. "
                + "The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.");
    }

}

你可能感兴趣的:(java,mysql)