问题描述
我正在使用来自包java.sql的接口连接
I am using the Interface Connection from the package java.sql
实际上,我虽然是一个Class,但是当我尝试查看源代码时却发现它是一个Interface.
Actually I though it a Class but when I tried to look at the source code I figured out that it is an Interface.
在Connection接口的源代码中,每种方法只有一行,没有任何实现!
In the source of the Connection interface there are only single line for each method without any implementation!!
是什么使该界面能够正常工作?
What makes this interface works as it?
要连接到的数据库:MySql
Database to connect to: MySql
连接源代码页: http://www.docjar.com/html/api/java /sql/Connection.java.html
推荐答案
它不是有效"的接口,而是其实现之一,特定于您的特定RDBMS供应商.实际上,通常是由供应商提供Connection
接口的实现.
It is not the interface that "works" but one of its implementations, which is specific to your particular RDBMS vendor. In fact, it is typically the vendor who provides the implementation of the Connection
interface.
致电时
Connection conn = DriverManager.getConnection(
"jdbc:jdbc:mysql://localhost:3306/
, connectionProps);
驱动程序管理器搜索已注册的JDBC驱动程序,找到用于MySQL的驱动程序(它从连接字符串中知道它是MySQL),将连接属性传递给实现Connection
的MySQL JDBC驱动程序内的类的构造函数,然后返回结果Connection
实例返回给您.例如,驱动程序可能返回实现Connection
的程序包专用类MySqlConnection
的实例,并且您的程序将使用它与RDBMS进行交互,而无需了解有关该类的任何详细信息,除了它实现了Connection
.
driver manager searches through registered JDBC drivers, finds the one for MySQL (it knows it's MySQL from the connection string), passes connection properties to the constructor of the class inside MySQL JDBC driver that implements Connection
, and returns the resultant Connection
instance back to you. For example, the driver may return an instance of a package-private class MySqlConnection
that implements Connection
, and your program would use it to interact with RDBMS without knowing any details about the class, other than the fact that it implements Connection
.
这篇关于java.sql.Connection接口的实现在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!