dcsimg
But Connection is a interface so we can't declare an object of interface & how can we access any method on refernce of connection
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Tuesday, January 29, 2008 02:50 PM

But Connection is a interface & so how can we access it

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection con=DriverManager.getConnection("jdbc:odbc:student");

Statement st = con.createStatement();

st.executeQuery("select * from A");

Re: But Connection is a interface so we can't declare an object of interface & how can we access any method on refernce of connection

Posted By:   Stephen_McConnell  
Posted On:   Tuesday, January 29, 2008 04:06 PM

When you Load the Class "sun.jdbc.odbc.JdbcOdbcDriver" you are loading a "concrete" instance of a JDBC driver into memory.


The "DriverManager" class is a "Factory" design pattern. When you pass it the parameter "jdbc:odbc" you are telling the Driver manager to find a class that fits those parameters. The DriverManager will then, find the "concrete class" create an instance of it... make the connection and pass you back a concrete instance of that connections.


The beautiful thing about Interfaces is that If the concrete class implements the methods that are described by the interface correctly, then you can call those methods using the call those methods based on the interface alone.


If you have no concrete class returned from the DriverManager, then you are out of luck.... and if you try to execute the methods, then you will get a "null pointer" exception.


Hope this helps.


Stephen McConnell

About | Sitemap | Contact