Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how java.sql.Connection works?

 
shan ta
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Trying from many years, but could not get the answer. please help me on this:

Where is the implementation details for java.sql.Connection interface?

If we want to write interface and hide its implementation, how this is possible? Please provide me a good example on this.
 
Imtiaz Ahmed
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Implementation for Connection Interface will be there in related Jars
means for Example if you are using database is Oracle 9i then it will be ojdbc14.jar
You can see in that jar for Implementation class
 
shan ta
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if Connection implementation is in that particular jar(ojdbc14.jar) then why are we creating a java.sql.Connection, meaning why are we using java api here?
please explain
 
shan ta
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as I was thinking I realised that the provider implements java.sql.Connection, and we create a child class object and store it in parent class reference(Connection).
your comments are much appreciated

thanks
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

as I was thinking I realised that the provider implements java.sql.Connection, and we create a child class object and store it in parent class reference(Connection).

What you are seeing is how interfaces work in Java. An interface provides a definition of the funtionality of something, whatever implements that interface actually provides the functionality itself. In this instance java.sql.Connection is the interface, the JDBC driver provides the implementation. So its not a child class; classes can't extend interfaces. The reference is of the type java.sql.Connection, but the class loaded by the classloader is the implementation.

For Oracle, the implementing class is called oracle.jdbc.driver.OracleConnection. Why would you not just use this class directly? The reason is the reason you would use any interface; by doing so you are not tied to any one implemwentation, so you can change database versions or types without changing your code.
 
shan ta
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that helps, Thank you!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic