JDBC- Implementation of interfaces

In JDBC you first register a driver by calling

Class.forName('classname')

which loads the Database class and registers that class with DriverManager

When you say DriverManager.getConnection() - It returns you java.sql.Connection (the contract as per specification)

Which class implements these methods?

The actual implementation is provided by the database vendor, for e.g. 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

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.


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.