tags:

views:

113

answers:

3

The following pseudocode works, which is probably no surprise to anyone who's done any JDBC work:

ResultSet rs = foo.executeQuery();
boolean hasNext = rs.next();
while(hasNext) {
    bar();
    boolean hasNext = rs.next();
}

ResultSet (java.sql.ResultSet) is an interface, so there should be no implementation for next(), only a method declaration. So how does the code run successfully? The best answer I found online is that what comes back is an instance of a class that implements the ResultSet interface.

That answer makes sense to me (it confirms what I assumed before I actually looked it up), but it only leads to another question: how do you know what kind of class you're getting back? Couldn't it, in theory, be something that overrides the interface's method in an unexpected or undesirable way?

+2  A: 

During Runtime your JVM knows what kind of class is returned. You can even access it yourself by using the java instanceof keyword.

It is perfectly possible that you get something that overrides this method in an undesirable way. But the signature of the method is always the same and only during runtime class cast problems are going to appear. This is the whole point of inheritance in an OOP language like Java.

More information here.

kazanaki
+1  A: 

What you get back is a class that implements those interfaces and that are found in your database driver.

For example if you connect to MySQL, you might use drivers from here.

The driver implements those interfaces. It does not implement the interfaces in an unexpected or undesirable way if you get them from the database providers (there are implementation variations between vendors of course but it should not be something major; e.g. I got burned today when using the DatabaseMetaData.getSQLStateType(). This should return 1 or 2, but my driver implementation decided to return 0, but with everything else I had no problems).

dpb
+1  A: 

By the way, this isn't related to your question, but I believe the recommended usage of a result set is supposed to go like:

ResultSet rs = conn.executeQuery();
while (rs.next()) {
   bar();
}

This leads to cleaner/shorter code, instead of having to use a temporary boolean variable.

On a more related note, you can generally trust interfaces to be implemented correctly if you use "brand name" implementors (in this case Oracle, MySQL, Microsoft, etc.) because of verbose documentation, large user communities, etc. Just like in the grocery store, only use generic products if you can confirm that what's happening inside is the same as what you expect from the brand names.

J. Dimeo
Thanks. You're right, of course. This is such an old question that I don't remember the reason I asked, but I bet I generified some debug code. I tend to intentionally break things apart too much when I debug so that I can step through more precisely.
Lord Torgamus