Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where are the implementation of java.sql.Connection interface? [duplicate]

Tags:

java

jdbc

I am using the Interface Connection from the package java.sql

Actually I though it a Class but when I tried to look at the source code I figured out that it is an Interface.

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?

Database to connect to: MySql

Connection source code page: http://www.docjar.com/html/api/java/sql/Connection.java.html

like image 722
malhobayyeb Avatar asked Dec 08 '22 22:12

malhobayyeb


2 Answers

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.

When you call

Connection conn = DriverManager.getConnection(
    "jdbc:jdbc:mysql://localhost:3306/
,    connectionProps);

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.

like image 198
Sergey Kalinichenko Avatar answered Dec 11 '22 10:12

Sergey Kalinichenko


The MySQL driver (http://www.mysql.com/products/connector/) contains the actual implementation. The java.sql.Connection interface just defines the methods that the JDBC standard expects. Each database driver must define how to actually connect.

like image 40
Glen Hughes Avatar answered Dec 11 '22 11:12

Glen Hughes