简体   繁体   English

JDBC-接口的实现

[英]JDBC- Implementation of interfaces

In JDBC, to connect and execute statements in DB we mainly make use of Connection,Statement and ResultSet which are interfaces.在 JDBC 中,连接和执行 DB 中的语句主要使用 Connection、Statement 和 ResultSet 这三个接口。 But their corresponding objects is later used to run methods like createStatement(),executeQuery(),next() etc.Which class implements these methods?但是它们对应的对象后来被用来运行createStatement()、executeQuery()、next()等方法。哪个类实现了这些方法? Why it is called as connection object instead of implemented class object?为什么它被称为连接对象而不是实现的类对象?

You don't actually do你实际上不做

Connection conn = new Connection();

You do something like你做类似的事情

Connection conn = DriverManager.getConnection(
     DB_PATH + "?user=" + USER_NAME + "&password=" + USER_PASS); 
Statement statement = conn.createStatement();
ResultSet resultSet = statement.executeQuery(query);

But to answer your question directly No you cannot instantiate an Interface you have to use a class that implements the Interface or use a Proxy.但是要直接回答您的问题,您不能实例化Interface您必须使用implements Interface的类或使用代理。

In JDBC you first register a driver by calling在 JDBC 中,您首先通过调用注册驱动程序

Class.forName('classname')

which loads the Database class and registers that class with DriverManager它加载数据库类并使用DriverManager注册该类

When you say DriverManager.getConnection( ) - It returns you java.sql.Connection (the contract as per specification)当你说DriverManager.getConnection( ) - 它返回你java.sql.Connection (根据规范的合同)

Which class implements these methods?哪个类实现了这些方法?

The actual implementation is provided by the database vendor, for eg Oracle, MySQL.实际实现由数据库供应商提供,例如 Oracle、MySQL。

Why it is called as connection object instead of implemented class object?为什么它被称为连接对象而不是实现的类对象?

Because you code to Interface and not implementation (good coding practice).因为您编码到接口而不是实现(良好的编码实践)。

If you want you can look up in the vendor jar and find which class implements Connection then instead of如果您愿意,您可以在供应商 jar 中查找并找到哪个类实现了 Connection 而不是

Connection connection = DriverManager.getConnection()

you can write你可以写

VendorConnectionImpl vendorConnection = (VendorConnectionImpl)DriverManager.getConnection()

This above will work but then it will bind you with that specific implementation.上面的这将起作用,但它会将您与该特定实现绑定在一起。

If you want to move from vendor1 to vendor2 you cannot do that, first you will have to change the above code as per vendor2 API, But if you use the first approach you can move from Vendor to Vendor without having pain of changing your code.如果您想从 vendor1 移动到 vendor2,您不能这样做,首先您必须根据 vendor2 API 更改上述代码,但是如果您使用第一种方法,您可以从供应商移动到供应商,而无需更改代码。

You can't instantiate an interface class.您不能实例化接口类。 You'll have either implement that interface yourself or use Proxy to create an instance of said interface that will delegate all calls to provided InvocationHandler .您要么自己实现该接口,要么使用Proxy创建该接口的实例,该实例将所有调用委托给提供的InvocationHandler

These guys are answering your question but I don't think that you are getting it from the comments.这些人正在回答你的问题,但我认为你没有从评论中得到它。

You need to actually implement the interface by creating your own class.您需要通过创建自己的类来实际实现接口。 So create a class that implements the interface, include the correct abstract methods, and then add whatever you want to the functionality in your new class.因此,创建一个实现接口的类,包含正确的抽象方法,然后在新类的功能中添加您想要的任何内容。 Then you'll instantiate that class to use the functions.然后您将实例化该类以使用这些函数。

An interface is like a programming "contract" it's there to make sure that any class used that extends the interface fulfills certain functions, however, you can't instantiate the interface itself to use its' methods, you inherit them/override them in your new class.接口就像一个编程“合同”,它在那里确保任何用于扩展接口的类实现某些功能,但是,您不能实例化接口本身以使用其方法,您可以在您的新班级。 After you override the abstract methods in your new class you can program in whatever else you want your particular interface extension class to do.在您覆盖新类中的抽象方法之后,您可以在您希望特定接口扩展类执行的任何其他操作中进行编程。

http://www.javabeginner.com/learn-java/java-abstract-class-and-interface and http://www.javaworld.com/javaworld/javaqa/2001-04/03-qa-0420-abstract.html will let you read up on these topics. http://www.javabeginner.com/learn-java/java-abstract-class-and-interfacehttp://www.javaworld.com/javaworld/javaqa/2001-04/03-qa-0420-abstract。 html将让您阅读这些主题。

Hope the following explanation will help.希望下面的解释会有所帮助。

Q: Where does the driver Connection implementation come from?问:驱动程序连接实现从何而来? And the implementation of Statement and ResultSet?而Statement和ResultSet的实现呢?

Class.forName("com.mysql.jdbc.Driver").newInstance();
// String url="jdbc:mysql://localhost:3306/mydb?user=root&password=root";
Connection con = DriverManager.getConnection(url);

Small tip: the driver implementation is loaded as soon as the DriverManager class is loaded(initialized).小提示:驱动程序实现在 DriverManager 类加载(初始化)后立即加载。

Take the source code of mysql-connector-java:5.1.36 for example.以mysql-connector-java:5.1.36的源码为例。

● class com.mysql.jdbc.Driver extends class NonRegisteringDriver , in which there is a method named connect() which returns an instance of Connection. com.mysql.jdbc.Driver继承了NonRegisteringDriver类,其中有一个名为connect()的方法,它返回一个 Connection 的实例。
pay attention to the registeredDrivers field of NonRegisteringDriver注意NonRegisteringDriver的registeredDrivers字段

public class Driver extends NonRegisteringDriver implements java.sql.Driver {
...
public java.sql.Connection connect(String url, Properties info) throws SQLException {

● mysql driver is registered and the driver is added to registeredDrivers : ● 注册mysql驱动,并将驱动添加到registeredDrivers

Class.forName("com.mysql.jdbc.Driver");
...
java.sql.DriverManager.registerDriver(new Driver()); // from com.mysql.jdbc.Driver
...
registeredDrivers.addIfAbsent(new DriverInfo(driver, da));  // mysql driver instance is added to registeredDrivers

from now on JDBC knows that there is one JDBC implementation, namely mysql.从此JDBC知道有一个JDBC实现,即mysql。
● Then create connection: ● 然后创建连接:

Connection con = DriverManager.getConnection(url);

● Follow the source code of DriverManager.getConnection ● 遵循DriverManager.getConnection的源代码
Pay attention to the registeredDrivers注意已注册的Drivers
Here connection implementation is created by aDriver.driver.connect这里的连接实现是由aDriver.driver.connect创建的

for(DriverInfo aDriver : registeredDrivers) {
...
     Connection con = aDriver.driver.connect(url, info);
...
}

● Since connection implementation for mysql has been found, the implementation of Statement and ResultSet can also be found by digging into the source code of mysql-connector-java:5.1.36 ● 既然找到了mysql的connection实现,那么StatementResultSet的实现也可以通过挖掘mysql-connector-java的源码找到:5.1.36

Statement stmt = con.createStatement();
// String sql = "select * from user";
ResultSet rs=stmt.executeQuery(sql);

As for JDBC 4 , driver implementation will be automatically detected from the classpath when the class DriverManager is loaded:对于JDBC 4 ,当加载类 DriverManager 时,将自动从类路径中检测驱动程序实现:
DriverManager.java#l101 DriverManager.java#l101

static {
    loadInitialDrivers();
    println("JDBC DriverManager initialized");
}

...

private static void loadInitialDrivers() {
...
    ServiceLoader<Driver> loadedDrivers = ServiceLoader.load(Driver.class);
...
}

content of file mysql-connector-java-5.1.36.jar!/ META-INF/services/java.sql.Driver : !文件使用mysql-connector-java的5.1.36.jar的内容/ META-INF /服务/ java.sql.Driver中

com.mysql.jdbc.Driver
com.mysql.fabric.jdbc.FabricMySQLDriver

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM