Why Class.forName('database driver')?
database, java, jdbc
Solution
With a JDBC 4.0 driver (and up), all you need is
dbConnection = DriverManager.getConnection(strUrl, props);
per the `DriverManager` javadoc,
JDBC 4.0 Drivers must include the file META-INF/services/java.sql.Driver. This file contains the name of the JDBC drivers implementation of java.sql.Driver. For example, to load the my.sql.Driver class, the META-INF/services/java.sql.Driver file would contain the entry:
my.sql.Driver
Applications no longer need to explictly load JDBC drivers using Class.forName(). Existing programs which currently load JDBC drivers using Class.forName() will continue to work without modification.
Problem
Why ``` Class.forName("org.apache.derby.jdbc.EmbeddedDriver"); dbConnection = DriverManager.getConnection(strUrl, props); ``` instead of ``` dbConnection = EmbeddedDriver.connect(strUrl, props); ``` ? Isn't it more error-prone to specify a string, instead of a class name that can be checked by the compiler? I saw an example where the class name was obtained from configuration, but this seems to be the pattern that is used regardless of available alternatives.