In JDBC, I see that Connection
is an interface, which defines methods for interacting with Database.
I also know that interface contains only abstract methods, and cannot be instantiated
But In JDBC code, how does the following work,
Connection connection = DriverManager.getConnection("URL String");
Statement statement=connection.createStatement();
As per my doubt createStatement()
should be a abstract method, i..e, without any body
But every thing works fine... with this
Can anyone explain?
DriverManager.getConnection
returns an object which implements the Connection
interface - behind the scenes there is a real object.
JDBC is like any driver mechanism - ahead of time the folks and Sun (now Oracle) defined the interface (or contract) that Java would use to interact with a database. It was then the responibility of the database vendors to provide implementations of that interface in order for their particular database to be used with Java.
The take away here is that the JDBC API defines the standard interface through which Java will interact with a database, the obvious benefit being that, providing your code only uses the JDBC API, it will be reasonably uniform regardless of the database you use, meaning you can therefore swap one implementation with another (although it's not always that simple in practice).
With regards to how implementations of the JDBC API are registered with the java.sql.DriverManager, what used to happen was that your code would need to explicitly load the class(es) implementing java.sql.Driver; the act of loading the class caused it to be registered with the java.sql.DriverManager. This mechanism has now be replaced with the service provider but the end result is still the same:
One or more implementations of java.sql.Driver are registered with the java.sql.DriverManager.
If you then look at the source code for java.sql.DriverManager.getConnection(String) you will see that it simply loops through the registered drivers until it finds one that accepts the connection url at which point the java.sql.DriverManager calls the java.sql.Driver.connect(String, Properties) method, returning a specific implementation of java.sql.Connection.
The whole point of defining abstract classes (or interfaces) is to have subclasses of the abstract class (or classes implementing the interface), providing an implementation for all the abstract methods of the abstract class (or of the interface). Otherwise, an abstract class or interface could never be used, and would be completely pointless.
So, what DriverManager.getConnection does, in fact, is the following:
return new MySqlConnection();
or
return new OracleConnection();
depending on the URL (this is an oversimplified explanation. In reality, it's a bit more complex than that).
and MySQLConnection and OracleConnection are concrete classes implementing the Connection interface:
public class MySQLConnection implements Connection {
...
}
An interface reference can point to any object of a class that implements this interface
i.e. see the example below:
interface Foo{
void display();
}
public class TestFoo implements Foo{
void display(){
System.out.println(“Hello World”);
}
public static void main(String[] args){
Foo foo = new TestFoo();
foo.display();
}
}