gpt4 book ai didi

java - MySQL JDBC 插件加载

转载 作者:可可西里 更新时间:2023-11-01 08:20:38 25 4
gpt4 key购买 nike

我在初始化 MySQL JDBC 连接的代码中看到过很多这种类型的代码。

Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://10.0.0.2:3306", "username", "password");

现在我的问题是为什么/什么时候需要第一行?通过这个,我已经能够毫无问题地连接到 MySQL 数据库:

conn = DriverManager.getConnection("jdbc:mysql://10.0.0.2:3306", "username", "password");

我正在使用 Java 7 和 Connector/J 5.0.8 驱动程序。

我的代码中没有任何直接加载 MySQL 驱动程序的导入或类加载语句,只有通用 Java SQL 接口(interface)类。

最佳答案

简答:使用 4.0 之前的 JDBC 驱动程序(JDBC 4.0 引入了 JDBC 驱动程序类的自动加载)

In previous versions of JDBC, to obtain a connection, you first had to initialize your JDBC driver by calling the method Class.forName. This methods required an object of type java.sql.Driver. Each JDBC driver contains one or more classes that implements the interface java.sql.Driver. The drivers for Java DB are org.apache.derby.jdbc.EmbeddedDriver and org.apache.derby.jdbc.ClientDriver, and the one for MySQL Connector/J is com.mysql.jdbc.Driver. See the documentation of your DBMS driver to obtain the name of the class that implements the interface java.sql.Driver.

Any JDBC 4.0 drivers that are found in your class path are automatically loaded. (However, you must manually load any drivers prior to JDBC 4.0 with the method Class.forName.)

来源:JDBC Basics

关于java - MySQL JDBC 插件加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13371803/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com