To connect to a database you probably read that you need a
driver. In general you need a driver specific to each database. A driver comes in the form of a jar file. What you probably have
not read is "What is a driver, exactly?" It is a collection of classes that implement all those interfaces!
To load a driver, you generally do something like
That generally loads a class (here "FooDriver") which implements the Driver interface. In a static initializer block, that class registers itself with the DriverManager, like
Now the DriverManager knows about the FooDriver; Driver has some methods the DriverManager can call to find out if a given Driver can connect to a given database. When the DriverManager wants a connection to the Foo database, it will ask the FooDriver class for a Connection :
But inside FooDriver, we see the implementation of connect:
and so your code ends up with a FooConnection object, but of course all you know is that it implements Connection. FooConnection doesn't even have to be a public class, and it's probably not. Then you call createStatement, and here's the code in FooConnection:
Do you see a
pattern here? There's a FooResultSet, and a FooResultSetMetadata, etc. You don't need to know the names of any of those classes -- they're always returned as interface types.
Neat, huh?