1

In JDBC when you need to get a connection, you have to load a class Driver first. You do it via invocation of Class.forName.

Class.forName("org.postgresql.Driver");

This class method load a class and add it to DriverManager holder:

static {
    try {
        DriverManager.registerDriver(new Driver());
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

Then to get connection you have to invoke a static method getConnection.

Connection connection = DriverManager.getConnection(DbUrl, Username, Password);

getConnection method creates a connection using separate thread and so on...

My question is why authors of JDK are using a static method: Class.forName, getConnection. What is the role of "static" approach?

barbara
  • 3,111
  • 6
  • 30
  • 58
  • I think the reason for the `Class.forName` is to verify the class can be found in the classpath - it doesn't load anything. Otherwise your program would fail when you use your specific jdbc connection – OneCricketeer Nov 02 '15 at 12:51
  • 1
    `Class.forName("org.postgresql.Driver");` is no longer required – Fran Montero Nov 02 '15 at 12:51
  • As for DriverManager.getConnection being static, why not? What would be the benefit of having to create an instance of DriverManager first? – Gimby Nov 02 '15 at 12:52
  • My question is related to the role of static methods in JDBC in general. Why we need to use them instead of instance methods. – barbara Nov 02 '15 at 12:54

1 Answers1

2

Previously you needed to explicitly load the class with Class.forName() because the drivers contained initialization code that would be run at class loading time (it's no longer required for new drivers).

DriverManager will register available Drivers, after which you can use the static getConnection() method to open a connection using any of the registered drivers. The driver chosen will depend on the jdbc URL used, and the static factory method will then return a "native" connection object that implements the Connection interface.

As you can easily see, there's no advantage to any of those operations being non-static, but after getting a Connection you no longer use static methods.

So Class.forName() is static for obvious reasons and DriverManager.getConnection() is static because it's a factory method for different JDBC driver Connection implementations.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • Why we cannot just use an instance method only? – barbara Nov 02 '15 at 12:59
  • Almost all method of `DriverManager` are static. – barbara Nov 02 '15 at 13:02
  • It's full of factory methods. It's possible to bypass `DriverManager` completely, but it would result in database dependent code and would be a bad idea generally. Multiple instances of `DriverManager` on the other hand would be unnecessary too. – Kayaman Nov 02 '15 at 13:03
  • > it would result in database dependent code – barbara Nov 02 '15 at 13:07
  • If I create a Driver via `new` (without a factory method) and invoke a `connect` method manually how it woould result in dependent code? `org.postgresql.Driver` – barbara Nov 02 '15 at 13:08
  • You would be using a class from the db-specific driver, instead of using only Java interfaces. In most cases it probably wouldn't make much difference, but with `DriverManager` it's done with respect to encapsulation. Therefore you don't *need* to know what's inside the driver jar, because you'll be using the standardized Java form. – Kayaman Nov 02 '15 at 13:12