0

I have a confusion regarding ResultSet . We know ResultSet is a interface , So it can be used as reference , but we cannot create its object . So my question is what object does Statement.executeQuery() returns ?

1

5 Answers 5

2

It returns a concrete object that implements ResultSet. For example, Oracle's implementation of the Statement interface, OracleStatement, returns an instance of OracleResultSetImpl.

2

You can get the name of the actual implementation class of ResultSet

Simply print

 ResultSet resultSet = ...

 System.out.println(resultSet.getClass().getName());
1

Well, Statement is also an interface. So if you've got a Statement object, then its class is actually something else, like maybe OracleStatement, or MsSqlServerStatement, or SybaseStatement or whatever; depending on which JDBC driver you have. (Actually, I kind of made up those class names - I don't know what the exact class names are, and it really doesn't matter).

Now, the version of executeQuery in each Statement implementation will be slightly different. The one in OracleStatement, for example, will do some stuff and create an OracleResultSet. The implementation in MsSqlServerStatement will do some slightly different stuff, and create a MsSqlServerResultSet. I'm still making up the class names, of course, but you get the point.

You never need to worry about these classes, because the programs that you write will just use the methods listed in the interfaces. This is exactly what interfaces are for.

0

You probably need to gain some more understanding of interfaces and their purpose. The general idea is that an interface is a way to encapsulate similar behavior between similar type objects, and you be able to use those generalized method with any implementation.

A simple case.

  1. interface Animal with speak() method
  2. Two different implementations Dragon and Dinosaur
public interface Animal {
    void speak();
}
public class Dragon implements Animal {
    @Override
    public void speak() {
        System.out.println("I'm a Dragon!")'
    }
}
public class Dinosaur implements Animal {
    @Override
    public void speak() {
        System.out.println("I'm a Dinosaur!")'
    }
}

In this case there is still a case or inheritance, with Animal being the super type. So Animal can be used as a data type. And calling its method speak(), will result in the actually calling it's implementation specified. For example

Animal animal = new Dragon();
animal.speak();  // prints I am a Dragon

Same is the case with ResultSet. When you call rs.next(), you are not calling an empty method,

public interface ResultSet {
    public boolean next();
}

but actually a next() method of some ResultSet[Impl] that has implemented the ResultSet interface

// I'm not sure the exact concrete implementation class, Just making on up
public class ResultSetImpl implements ResultSet {
    @Override
    public boolean next() {
       return iStillHaveMore();
    }
}

So if statement.executeQuery() returns ResultSetImpl, then that's the next() method you'd be using

1
  • This analogy would have been so much better if you'd made speak return an interface Speech, implemented by (for example) Growl in the Lion class and Fire in the Dragon class. May 4, 2014 at 8:25
0

A ResultSet contains records which contains a set of columns. Each record contains exactly the same amount of columns, although some columns may have no value.

When you run Statement.executeQuery() the driver you're using to connect to your database, create that object for you. So that you can call its methods such as next(), getString(), getInt() etc.

To know which is returned object, you can call rs.getClass().getName(), assuming rs is the variable of your ResultSet. But you don't really need to know that. That's why you're using a Instance, and the scope of Instance as well.

Unless you're writing a driver library, you'll never create a ResultSet object, nor you have the need to do that.

1
  • A ResultSet contains records which interesting story but not related to OP's question!
    – VH-NZZ
    May 4, 2014 at 8:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.