Implementation of Java JDBC interface

In JDBC, in order to connect and execute statements in dB, we mainly use connection, statement and resultset as interfaces But their corresponding objects are later used to run methods such as createstatement(), executequery(), next() Do these classes implement these methods?

Solution

In JDBC, you first register the driver by calling

Class.forName('classname')

It loads the database class and registers it using drivermanager

When you say drivermanager Getconnection () – it returns Java sql. Connection (contract according to specification)

The actual implementation is provided by database vendors, such as Oracle, mysql

Because you code to interface and not implementation

If you want, you can find which class implements connection in the vendor jar instead of

Connection connection = DriverManager.getConnection()

You can write

VendorConnectionImpl vendorConnection = (VendorConnectionImpl)DriverManager.getConnection()

The above will work, but it will bind you to that particular implementation

If you want to migrate from vendor1 to vendor2, you cannot perform this operation. First, you must change the above code according to the vendor2 API. However, if you use the first method, you can transfer from vendor to vendor without changing the code

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>