Loading JDBC drivers
I split these examples into two specific categories which are good and dumb.
For executing this, you should write
java -Dmy.driver=oracle.jdbc.driver.OracleDriver dr3
The -D option is followed by the property name (without space) and =followed by the driver class. Don't forget the class name. It is damn important!
Note: The second, third and fourth ways have a common problem. Here if we aren't decisive about which driver to use and give it at runtime we need to change the URL (to connect to the database) too. Because, different drivers have different urls. So, there must be some conditional logic written which connects to the database via appropriate URL corresponding to the given class name. This becomes a bit messy. Isn't it?
Well, where is the code? I'll explain, everything lies in executing the program. You should execute this in this way.
java -Djdbc.drivers=oracle.jdbc.driver.OracleDriver dr4
Now, this loads the driver. The jdbc.drivers is an environment property of Java that corresponds to the JDBC drivers. The default value for this property is null. While executing, you are changing its value to the driver class name.
3 Dumb ways
I call this dumb because, we are creating an object for this class which is absolutely unnecessary. Here is a simple question to you
Why create an object for a class when you are not going to use it? If you want to load the static block of the class? Isn't it enough to load the class into the memory using Class.forName() instead of creating an object for it?
This way is double-dumb because you are loading the driver twice. Yes, thestatic block of the oracle.jdbc.driver.OracleDriver already contains a call to the registerDriver(). The statement, new oracle.jdbc.driver.OracleDriver(); loads it into memory (therefore, static block is executed) and creates an object for the driver class and then register the driver.