| Author |
jdbc
|
sri jaisi
Greenhorn
Joined: Sep 02, 2006
Posts: 23
|
|
to access db through java try { class.forName("") Connection con=driverManager.getConnection(""); Statement st=con.createstatement(); .. } here except driverManager all are interfaces;in interfaces all the methods are implictly public and abstract but not static;then how we are accessing them;which class implements these interfaces thankz sri
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24057
|
|
A JDBC driver is nothing more nor less than a collection of classes that implement those interfaces. The DriverManager is a real class: when a driver is loaded using Class.forName() it registers itself with the DriverManager. When you call DriverManager.getConnection(), it gets an instance of an implementation of the Connection interface by asking the appropriate Driver implementation. The large advantage of this system is that the JDBC code you write is completely independent of the driver -- it doesn't include the names of the driver classes.
|
[Jess in Action][AskingGoodQuestions]
|
 |
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Joined: Aug 26, 2006
Posts: 4967
|
|
Yes, we are programming to an interface in our code, but behind the scenes, there are real, solid, hard-bodies Java objects doing the work. Even though we are using the Connection interface, the Driver.getConnection method is instantiating a class, based on teh driver name I believe, that implements the connection interface. If I do a getClass call on the Connection object, and I use db2 all the time, it will describe a class of type com.ibm.db2.jdbc.driver.oogle.moogle.IBMDabaseRealNewableClass, or something like that. It is an interface we are programming to, but there is a real class doing the work. It's the same thing with the statement. The staement is an interface, but the connection object is returning a real, tangible class that simply implements the interface. So, programming to interfaces is good, and makes things very flexible. Cheers! -Cameron McKenzie
|
Author of Hibernate Made Easy, What is WebSphere???, JSF 2.0 Made Easy and the SCJA Certification Guides
|
 |
sri jaisi
Greenhorn
Joined: Sep 02, 2006
Posts: 23
|
|
thankz ernest thankz kameron
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
Originally posted by Kameron McKenzie: Even though we are using the Connection interface, the Driver.getConnection method is instantiating a class, based on teh driver name I believe, that implements the connection interface.
In fact what happens is that the DriverManager asks the registered Drivers whether they can handle the JDBC URL. It simply uses the first it encounters that says it can. For the details of how this is implemented, look at the source code that is coming with the JDK.
|
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
|
 |
Paul Sturrock
Bartender
Joined: Apr 14, 2004
Posts: 10336
|
|
|
Moving to JDBC forum...
|
JavaRanch FAQ HowToAskQuestionsOnJavaRanch
|
 |
 |
|
|
subject: jdbc
|
|
|