2

I've come across something new today and can't seem to wrap my head around it. Please bear in mind that the follow question applies to Java - mainly Java JDBC.

I have the following code:

Connection con = DriverManager.getConnection(Url, "username", "password");

And this:

Statement myStmt = con.createStatement();
ResultSet myRs = myStmt.executeQuery("select * from employees");

What is actually going on here? I gather that the method returns an object such as a new Connection object?

Also, isn't Connection an interface? So how would it create a new object?

Snessy
  • 33
  • 6
  • "I gather that the method returns an object such as a new Connection object?" - you have posted code with three different method calls; are you talking about the first one? then what is the relevance of the other two in your question? – davmac Dec 04 '14 at 21:11
  • Also: "Also, isn't Connection an interface? So how would it create a new object?" - Connection is an interface yes, but Connection doesn't create any object. The object comes from the `DriverManager.getConnection(...)` method (in the DriverManager class). – davmac Dec 04 '14 at 21:13
  • Sorry I should of clarified about those. I was wondering about all 3 as they seem to be using different classes to assign objects? – Snessy Dec 04 '14 at 21:13
  • "Sorry I should of clarified about those" - ok, so do it now, by editing your question. – davmac Dec 04 '14 at 21:15
  • BTW... welcome to Stack Overflow – Mark Giaconia Dec 04 '14 at 21:19

5 Answers5

1

In interface describes which methods signatures are available for a specific object. What you do in this line

Connection con = DriverManager.getConnection(Url, "username", "password");

is that you assign the instance created using the getConnection factory method to a reference con that can hold instances that comply with the Connection interface.

You cannot directly instantiate interfaces - as interfaces don't specify an implementation. You can however treat the references to an object that implements the interface as you do references to other objects. If you strictly keep to the methods defined in the interface you can swap one object with another as long as they implement the interface, during compile time or runtime.

A factory method such as getConnection() is a static method that is used to instantiate objects. Objects can also be created using constructors, but factory methods are more flexible and may be even be defined within any class (such as DriverManager in the example).

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
1

The code:

Connection con = DriverManager.getConnection(Url, "username", "password");

This is a Factory Pattern. The manager creates the proper concrete Connection class based on the URL passed in.

Connection itself is an interface (javadoc) that all of the concrete connection classes implement. MySQL, Postgres, Oracle, whatever... they are all Connection classes. The DriverManager selects the right one of those and creates it.

How does the DriverManager know? The Driver classes register themselves with the Driver when they are loaded by the class loader. When the driver goes through asking "who responds to this URL" it invokes the acceptsURL method in each driver and the one that responds gives it a connection back when called with connect.

Once you have a Connection object from the Driver (that knows how to talk to the database the driver works with) it is then able to create a statement or execute a query against that database that returns back a result set.

The specifics of these are up to the driver to implement. You've just got interfaces to these way that it works. All of the ResultSets and Statements and Connections (and Drivers) work the same way from your point of view, though how they connect to the cursor and database behind the scenes may be different. But that doesn't matter. You've got an interface and they all work the same way.

These are all instances of Factory Patterns. Factory, Abstract Factory and Factory Method may be a useful read on the matter. Other Patterns are described Examples of GoF Design Patterns in Java's core libraries. You may also find This lecture on Factory Pattern to be useful (its got the classic 'pizza' example).

As these are Patterns (and I intentionally write that with a capital 'P'), you will find these solutions used again and again in similar ways throughout programs. Once you recognize that you are dealing with a Factory, you will likely be able to work with it more efficiently.

Community
  • 1
  • 1
0

Java methods have "return types," so you can call a method that returns an object. In both cases here,

 Statement myStmt = con.createStatement();
    ResultSet myRs = myStmt.executeQuery("select * from employees");

the methods are returning objects, one is a Statement, and the other is the resulting data from executing the statement (ResultSet)

Hope that helps, I'm assumin you're new to OOP, this is a common pattern you will use and implement.

Mark Giaconia
  • 3,844
  • 5
  • 20
  • 42
0

Connection con = DriverManager.getConnection(Url, "username", "password");

1) Yes, the class "DriverManager" has a static method that returns a "Connection" object.

2) Yes, "Connection" is an interface. The object returned by getConnection() is an instance of a class that implements that interface.

Here is the documentation for DriverManager, Connection, and Driver, respectively:

http://docs.oracle.com/javase/7/docs/api/java/sql/DriverManager.html

http://docs.oracle.com/javase/7/docs/api/java/sql/Connection.html

http://docs.oracle.com/javase/7/docs/api/java/sql/Driver.html

The key thing to note is that you want to dynamically load the correct driver at runtime. You might have an Oracle driver, or an MSSQL driver, or a mySQL driver. That will be transparent to almost all of your JDBC code - except for the parts that 1) load the driver, and 2) set up the specific connection string.

'Hope that helps!

FoggyDay
  • 11,962
  • 4
  • 34
  • 48
  • "the class "Driver" has a static method" - the class is named `DriverManager` not `Driver`. – davmac Dec 04 '14 at 21:13
0

Here yes you are getting the objects and when it comes to Connection it is a interface and you get the implementation of the methods in Connection interface by calling DriverManager.getConnection(Url, "username", "password").

Basically this is another use of interface, the implementation is hidden you just know that there is a method by this name in this interface and you can use it.

piyush
  • 115
  • 2
  • 12