• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Interface ResultSet

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I have a question. I thought interface by definiton is abstract and therefore cannot be instantiated. Also, the body of interface is usally a list of method protypes and you have to provide specific implementations for it, am I correct?
I came across Interfact ResultSet in jdk1.4, and I'm able to do
ResultSet rs = stmt.executeQuery( "SELECT * FROM students" ) ;
and then use a loop to get the data without the need to implement the ResutlSet myself. I try to use Interface RowSet at first, but from tutorials, it looks like I have to implement RowSet like CachedRowSet and etc first, so I'm very confused and I think my concept has problems.
If anyone can explains this to me a little, I would appreicate it. Thanks a lot.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ResultSet has been implemented but not by you. It was implemented by the provider of the Statement object that you are using in that instruction. In fact, Statement is also an interface.
Here's a real simple example:

If you only wrote InterfaceTest, what would you know about MyResultSet? MyResultSet would be a complete mystery to you wouldn't it? That is exactly how it works in the real JDBC versions. Someone else wrote an implementation of ResultSet and you are using it without even knowing it!
[ April 22, 2003: Message edited by: Thomas Paul ]
 
Liming Xu
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, it makes sense, but now I have another question in reaction to your description.
So the Interface Statement is able to implement another interface like ResultSet here? I thought the only thing you are able to do wiht an interface is to extend another interface without any real implementation inside. Where is the real implementation of the executeQuery()? Inside Interface Statement class itself or somewhere else? Sorry for the stupid question, not sure if it makes any sense. For example
I can understand if it's like this
class Statement
{
public ResultSet executeQuery(String s)
{
....
return ....
}
}
but
Interface Statement
{
public ResultSet executeQuery(String s);
// but where is the implementation??
}
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe this will make it clearer. Here is a really bad implementation of a ResultSet that never returns any rows.

The only real class that you see is DriverManager.
[ April 22, 2003: Message edited by: Thomas Paul ]
 
Liming Xu
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry about the last post, I just did a little trace (which I should've done before I posted), it looks like
ResultSet (an Interface) gets the implementation from Statement(an Interface too)
and Statement(Interface) gets from Connection(an Interface as well)
Connection(Inteface) gets from DriverManager.getConnection() , here DriverManager is a class. So are we saying that DriverManager implemented most of the interface methods?

-------------
Oh, just saw your post..
Right, if Statement is a class, then it's understandable... but in the Jdk API, Statement is an Interface, that's why it throws me off...
[ April 22, 2003: Message edited by: Liming Xu ]
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
DriverManager doesn't implement any of the interfaces. DriverManager knows the name of all the classes that have implemented the Connection class and registered with the DriverManager. It determines which one to use based on the URL you pass. In our example, DriverManager would know that MyConnection implements Connection and gives that to you. You use MyConnection to get a MyStatement. You use MyStatement to get a MyResultSet. But you never see any of those classes. All you see is Connection, Statement, and ResultSet. That means if you switch to different classes that are called MyConnection2, MyStatement2, and MyResultSet2 you don't have to make any changes to your code!
I made a change to the example above to make Statement an interface.
[ April 22, 2003: Message edited by: Thomas Paul ]
 
Liming Xu
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
THANKS A LOT Thomas, now I get the idea, finally! heheh... That's such a GREAT example. Again, thank you so much, now I understand why Interface is important.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic