It returns a concrete object that implements ResultSet
.
For example, Oracle's implementation of the Statement
interface, OracleStatement
, returns an instance of OracleResultSetImpl
.
ResultSet is a interface. How does Statement.executeQuery(String) create its object.?
-
15-07-2023 - |
Pergunta
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 ?
Solução
Outras dicas
You can get the name of the actual implementation class of ResultSet
Simply print
ResultSet resultSet = ...
System.out.println(resultSet.getClass().getName());
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.
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.
interface Animal
withspeak()
method- Two different implementations
Dragon
andDinosaur
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
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.