Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jdbc

 
sri jaisi
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
sri jaisi
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thankz ernest
thankz kameron
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to JDBC forum...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic