http://java.sun.com/j2se/1.4.2/docs/api/ According to the API : class java.sql.DriverManager only extends Object，implements no interface 「public class DriverManager extends Object」 If it's true, why can we use createStatement() method in interface Connection ??? ---------------------------------- ...... Connection con=DriverManager.getConnection("........"); Statement stmt=con.createStatement(); //why??? ...... ------------------------------------ WHY ?? Method createStatement() declared in interface Connection, it doesn't implemented by any class and has no method body, why we can use this method ??? Thanx of your help in advanced !! [ April 15, 2004: Message edited by: ChenSanHau ] [ April 15, 2004: Message edited by: ChenSanHau ]
"ChenSanHau", We're pleased to have you here with us in the JDBC forum, but there are a few rules that need to be followed, and one is that proper names are required. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it. In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious. Thanks! bear Forum Bartender [ April 15, 2004: Message edited by: Bear Bibeault ]
The answer to your question is that the DriverManager.getConnection() method return an instance of an object that implements the Connection interface.
Joined: Feb 06, 2004
Thanks, dear Bear Bibeault !! I would change my name with inserting a space character later!! Although the DriverManager.getConnection() method return an instance of an object that implements the Connection interface, I still have a question: the Connection interface doesn't have any method body, how can we create an instance?? There is no codes to run in that method !!! I write a sample to simulate :
OK, the whole idea of interfaces is pure basic Java and so I'm going to move this topic to the Java in General(intermediate) forum since this is isn't really about JDBC. If you search through that forum, you will find topics that have explained the concept of interfaces.
The Connection Interface is implemented by specific classes for each JDBC driver. The DriverManager will determine which exact specific class to return and create an instance of that class. That specific class will have an implementation of the method because it implements Connection Interface which basically tells you that any class implementing that Interface HAS to implement that method (and all the others). Of course the implementation might throw an exception but a JDBC driver that cannot create a Statement is pretty much useless so I don't expect any to be broken like that.
Interface in JDBC is slightly different from the ones you see in the other package, for example, in java.util.*, there are interfaces, let say List or Map, and you can see the some classes implement them, like ArrayList, HashMap etc.. The implementations of these JDBC interfaces are "hidden", they are in your JDBC driver, and they are DB vendor specific.