0

I know that in order to connect java code to mysql we have to load com.mysql.jdbc.Driver by any one of the folllowing options we have(i am not sure about all of them) :-

Class.forName("com.mysql.jdbc.Driver");
OR
Class.forName("com.mysql.jdbc.Driver").newInstance();
OR
System.setProperty("jdbc.drivers","com.mysql.jdbc.Driver");
OR 
//through command prompt
java -Djdbc.drivers=com.mysql.jdbc.Driver ProgramName
Or
DriverManager.registerDriver("com.mysql.jdbc.Driver");

My Questions :-

First Question- I want to confirm that are all of the above methods legal to load the driver class.

Second Question:- The second method that we have, i.e.

Class.forName("com.mysql.jdbc.Driver").newInstance();

is same as doing object creation by using new keyword. For eg.

new com.mysql.jdbc.Driver();

I have tried through this and it works. So, why can't we use the second approach? Is there any disadvantage of using it.?

I think i have made my both questions clear , if not, please comment, i will edit it.

PeterMmm
  • 24,152
  • 13
  • 73
  • 111
sagar
  • 725
  • 2
  • 13
  • 30
  • 3
    Because you may not know what database you are connecting to. The URL and class name may be provided as a `String`, via configuration file – MadProgrammer Sep 18 '14 at 06:12
  • http://stackoverflow.com/questions/5484227/jdbc-class-forname-vs-drivermanager-registerdriver – PeterMmm Sep 18 '14 at 06:39
  • If driver is static (non-configurable) I mostly use `Class.forName(com.mysql.jdbc.Driver.class.getName())`. – PeterMmm Sep 18 '14 at 06:41
  • 3
    You shouldn't use either. When using a JDBC4 capable driver it should be automatically registered throught the service provider API. There should be a file called `java.sql.Driver` in the `META-INF/services` directory which contains the name of the JDBC driver to load. You should simply use the `DriverManager` to get the connection (or use a proper connection pool imlpementation). – M. Deinum Sep 18 '14 at 06:44
  • @M.Deinum what actually happens if we have more than 1 driver i.e. 1 mysql and 1 for oracle connectivity. – sagar Sep 18 '14 at 07:07
  • 3
    The actual driver used is detected on the jdbc url you are using to connect. `jdbc:mysql:...` will use the MySQL driver whereas `jdc:oracle:...` will use the Oracle driver. So when using the `DriverManager` you should only need to do `DriverManager.getConnection("jdbc:mysql://yourserver")` to get the connection. Nothing more, nothing less. People should stop loading drivers themselves as there is no more need to do so. – M. Deinum Sep 18 '14 at 07:12
  • @M.Deinum as u have said that there is no more need to use **Class.forName()** now. But I want to ask you one more thing that why it is used before, I mean does there any difference between Class.forName("com.mysql.jdbc.Driver").newInstance(); and new com.mysql.jdbc.Driver(); – sagar Sep 18 '14 at 07:33
  • Once upon a time there was no such thing as the service providers in java hence one needed to manually load the drivers. The main differences betweeen the `Class.forName` and the `new Driver()` is the latter is statically linked to the driver (resulting in a classnotfound if you start yuour application, the other will result in a classnotfound also but not kill your application, you could load another driver if you want). – M. Deinum Sep 18 '14 at 08:12

1 Answers1

3

As of JDBC 4.0 and the Service Provider API in Java you as a developer shouldn't load the drivers anymore. Any JDBC 4 capable driver should provide a file containing the actual driver class to load. The java.sql.DriverManager will use the Service Provider to load all available Driver classes in the classpath.

So although all the ways you showed are valid you shouldn't use them anymore. I would consider tutorials that still use that way of loading drivers obsolete. Even H2 and Derby provide this mechanism (at least MySQL, Oracle and Microsoft also support this also). So the only thing, when using a DriverManager you should do in your code is

DriverManager.getConnection("jdbc:mysql://localhost/yourdb");

To answer your second question the difference between Class.forName("com.mysql.jdbc.Driver").newInstance(); and new com.mysql.jdbc.Driver(); is a bit more difficult as you get into static linking and classloading. In the end you end-up with the same result an instance of the com.mysql.jdbc.Driver.

However the new com.mysql.jdbc.Driver(); will lead to a static link to this class as it will be part of your classes import statements. So as soon as you load this class and the com.mysql.jdbc.Driver class isn't available it will crash.

Whereas the Class.forName("com.mysql.jdbc.Driver").newInstance(); will at runtime try to load the class. It will also fail with a classnotfound but you could catch it and try to load another driver or show a nice message to the user.

But as mentioned you shouldn't be doing either of those and let the JDBC drivers auto-load.

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
  • What do you mean by **Class.forName("com.mysql.jdbc.Driver").newInstance(); will at runtime try to load the class** The new operator in java will also load classes at runtime. So whats the basic difference ,, Sorry i didn't get this properly, please elaborate this point. – sagar Sep 18 '14 at 08:47
  • Just as I say. If you put a try catch around it it will still throw an exception but won't hold your application. The `new` one will prevent the application from being loaded at all. – M. Deinum Sep 18 '14 at 08:48