You are on page 1of 5

In JDBC, to connect and execute statements in DB we mainly

make use of Connection,Statement and ResultSet which are


interfaces. But their corresponding o!ects is later used to run
methods like createStatement"#,execute$uery"#,next"# etc.%hich
class implements these methods& %hy it is called as connection
o!ect instead of implemented class o!ect&
In JDBC you first register a dri'er y calling
Class.forName('classname')
which loads the Dataase class and registers that class with
DriverManager
%hen you say DriverManager.getConnection(# ( It returns you
java.sql.Connection "the contract as per specification#
%hich class implements these methods&
)he actual implementation is pro'ided y the dataase 'endor, for
e.g. *racle, +yS$,.
%hy it is called as connection o!ect instead of implemented class
o!ect&
Because you code to Interface and not implementation "good
coding practice#.
If you want you can look up in the 'endor !ar and find which class
implements Connection then instead of
Connection connection = DriverManager.getConnection()
you can write
VendorConnectionImpl vendorConnection =
(VendorConnectionImpl)DriverManager.getConnection()
)his ao'e will work ut then it will ind you with that specific
implementation.
If you want to mo'e from 'endor- to 'endor. you cannot do that,
first you will ha'e to change the ao'e code as per 'endor. /0I,
But if you use the first approach you can mo'e from 1endor to
1endor without ha'ing pain of changing your code.
JDBC API is mostly consisting of interfaces. So that most
commonly used objects are the implementation of these
interfaces. As u will see that Connection Statement
PreparedStatement CallableStatement !esultSet etc.
are all interfaces. So who pro"ides the implementation#
$he answer is that the JDBC dri"er is what pro"ides the
implementation of all these interfaces.
%hen you ma&e a call as '(
Dri"er)anager.getConnection*+db,rl+ +username+
+password+-.
$he Dri"er)anager class chec& out with all the registered
dri"ers if they recogni/e the db,rl. If any of the dri"ers
recogni/s the db,rl then the Dri"er)anager uses that
Dri"er class to get the Connection object. $he Connection
object in this case is the implementation of the
Connection interface as pro"ided by the dri"er. Similarly
the implementation for all other interfaces li&e
Statement !esultSet etc are pro"ided by the Dri"er.
Sun pro"ides just the set of interfaces in the ja"a.s0l
pac&age and any third(party people can implement it
based on the spec
$he +magic+ behind the way that JDBC wor&s is that you
as& the Dri"er)anager for a connection using a ,!1.
$heDri"er)anager gi"es you a concrete class that
implements the Connection interface. 2ou then turn
around and as& the concrete class that implements the
Connection interface for a Statement so that concrete
class returns another concrete class that implements the
Statement interface. 3otice that in this whole scenario
that you ne"er +new+ anything *that is you ne"er write
+444 5 new 666+-.
It is not the interface that 2works2 ut one of its implementations,
which is specific to your particular RDB+S 'endor. In fact, it is
typically the 'endor who pro'ides the implementation of
the Connection interface.
%hen you call
Connection conn = DriverManager.getConnection(
"jdc!jdc!m"sql!##local$ost!%%&'#
( connection)rops)*
dri'er manager searches through registered JDBC dri'ers, finds
the one for +yS$, "it knows it3s +yS$, from the connection
string#, passes connection properties to the constructor of the
class inside +yS$,(JDBC dri'er that implements Connection,
and returns the resultant Connection instance ack to you. 4or
example, the dri'er may return an instance of a package(pri'ate
class M"+qlConnection that implements Connection, and your
program would use it to interact with RDB+S without knowing any
details aout the class, other than the fact that it
implements Connection.
)he Dri'er+anager does not know which is the +yS$, dri'er, it
will simply 5uery each registered Dri'er and ask if that Dri'er will
accept the 6R, "using accepts,-.#, the first to return true on
that 5uery will e asked to create the connection.
Sun "now oracle# pro'ides the spec or /0I as we call it as a set of
interfaces... the 'endor takes the /0I, writes implementations for
those ecause only they ha'e the clear knowledge of how to
implement them for their dataase and pro'ide us the type 7
dri'er. 8ou ha'e to use the dri'er to connect to the DB and
perform 'arious operations like creating connections, executing
S$, statements and so on and so forth. !a'a.s5l.9 is a great
example of design y interface.
JDBC is a standard. It defines a set of interfaces. )hen each
JDBC dri'er ( that is, a concrete implementation for a particular
dataase, has to follow exactly these interfaces.
)hen the dri'er registers itself in the dri'er manager "pre'iously
you had to do this withClass.forName(..), and when you
call DriverManager.getConnection(..), the proper dri'er is
chosen ased on the passed url, and it instantiates a connection.
if you output connection.getClass() you will
get com.m"sql.driver.ConnectionImpl "for example#. )his
means that the !dc dri'er has pro'ided an implementation of
the Connection interface, ut you don3t need to e aware of the
classes of each dri'er ( you only need to know the JDBC
interfaces.

You might also like