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?