File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Statement object a Inteface or class? 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 "Statement object a Inteface or class?" Watch "Statement object a Inteface or class?" New topic

Statement object a Inteface or class?

vijayanand gandhi

Joined: Oct 28, 2003
Posts: 1
Is Statement object an interface or class? if its interface then all methods have to be implemented but we just cteate and start using its methods, if its a class then it has to be instantiated which we dont do.
Then what exactly is a statement object and why?
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

One of the strengths of JDBC is that it is all based on interfaces. Interfaces allow you to define the behaviour of a class without defining the specifics of how thet behaviour is implemented.
One of the only concrete classes you usually deal with in JDBC is the DriverManager, and everything after this in terms of interfaces. Note that a Connection is also an interface. When you call DriverManager.getConnection, it takes care of the object creation and passes a Class to you.
Although it passes you a real class, you only know it behaves like a Connection. You are only interested in its Connection-like behaviour. It is possible to find out what the hidden class is, and it is even possible to cast to this hidden type, but this is almost always a bad idea.
Now we have a class but we are not sure what the exact type is, but we know it behaves like a Connection. One of the behaviuours of a Connection is that using createStatement() will pass you something that behaves like a Statement.
Again a Statement is an interface and all we know is that there is a concrete class hidden from us that behaves like a Statement, but all we care about is that it behaves like a Statement.
Where did this statement come from?
Although the Connection is an interface, in reality it is a hidden concrete class. When call createStatement on the interface, you are really calling createStatement on the class, and this class knows how to create the sort of Statement that it needs to execute statements.
If you run Oracle, you get Oracle Connections and Statements, if you run MySQL, you get MySQL Connections and Statements.
I hope this helps.
I agree. Here's the link:
subject: Statement object a Inteface or class?
It's not a secret anymore!