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.