This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
The Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); is used load the class run time. A new Object creation does the same . Then what is the excat difference between these two. The JdbcOdbcDriver class has static method where it registers the driver DriverManager.registerDriver(jdbcodbcdriver); If I can call the same stuff using the object why i need the class loader ? thanks
champak Welcome to the Java Ranch, we hope you’ll enjoy visiting as a regular however, your name is not in keeping with our naming policy here at the ranch. Please re-register under an appropriate name as shown in the policy. Thanks again and we hope to see you around the ranch!!
For example, using Class.forName() allows you to load the name of the required driver from a properties file at runtime. The separates the database implementation from the java code. Also Class.forName() throws ClassNotFoundException allowing you to make your code more robust if the required drivers have not been deployed correctly.
Joined: Feb 07, 2002
hi Graeme , Thanks for the reply , but can u please let me know the difference between the process of class loaders and new object creation ? Thanks Champak
Joined: Oct 13, 2000
Originally posted by champak: hi Graeme , can u please let me know the difference between the process of class loaders and new object creation ? Thanks Champak
In terms of making the driver available there is no difference. As you correctly stated in your original post, it is a requirement for the driver to register itself with the driver manager when it is loaded into memory. The loading can be achieved in any way you like. However, for the reasons I mentioned in my last post, using Class.forName() is the generally accepted and preferred way of achieving this.