jQuery in Action, 3rd edition
The moose likes JDBC and Relational Databases and the fly likes Some issues related with interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Some issues related with interfaces" Watch "Some issues related with interfaces" New topic

Some issues related with interfaces

Simrat Singh

Joined: Mar 31, 2007
Posts: 6
Dear friends,
I am a newbee in java and i was working with JDBC, i found a very strange thing or may be i dont hav that much knowledge till now. When we eastablish a connection we use atleast three different objects of the following classes :

1) Connection
2) Statement
3) ResultSet

Now as-per my knowledge all the above three are interfaces and in our programs we call different methods using these three objects. Calling a method using an interface object is only possible when the interface object reffers to the object of a class which implements that interface. But these objects dont have the reffernce to any class which implements them so how are we able to call the methods using these interface objects ??

Please help me keeping this thing in mind that i am a newbee.

Thnaking you all
Simrat Singh
Pravin Jain
Ranch Hand

Joined: May 20, 2007
Posts: 60
Hello Simrat,

Most of the JDBC api which is part of JDK is full of interfaces. There
are different implementations of these interfaces done by different
JDBC driver vendors. We need not worry about the actual implementing
classes since each non-abstract implementating class will have
implementation of the methods and we may invoke these methods.

The Zen of Java Programming.
Simrat Singh

Joined: Mar 31, 2007
Posts: 6
You are quite right dear but still there are somethings which make me confuse. Some of them are the following

Connection con = DriverManager.getConnection
( "jdbc:myDriver:wombat", "myLogin","myPassword");

Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM Table1");
while (rs.next()) {
int x = rs.getInt("a");
String s = rs.getString("b");
float f = rs.getFloat("c");
Now in the above code talking about the first line, the DriverManager.getConnection() method returns a Connection object only, so how is it possible to call any method using that connection object.
Now there is one solution in my mind which i think may be the case, what i think is that may be in the getConnection method a Connection object gets created and that connection object is being assinged a refference of any class which implements Connection interface. After that this connection object is returned by the getConnection method.
Now please let me know if i am thinking right and please keep in mind that i m a newbee so please explain thoroughly.

Thanking you
Simrat Singh
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
You have to distinguish between an interface (like Connection) and a class that implements that interface. There is no "interface object", not does an interface "refer to an object".

What DriverManager.getConnection does is to return an object that implements the Connection interface. If you're curious what kind of object it is, you can use con.getClass().getName() to find out. The same goes for the stmt and rs objects - they implement their respective interfaces, but the actual implementing class will be different depending on which driver you use.

The "con" object may implement many more methods, but you can access only the ones that are part of the Connection interface.
Simrat Singh

Joined: Mar 31, 2007
Posts: 6
Thanks Sir
I agree. Here's the link: http://aspose.com/file-tools
subject: Some issues related with interfaces
It's not a secret anymore!