File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Loading a driver Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Loading a driver" Watch "Loading a driver" New topic

Loading a driver

Parijat Mukherjee

Joined: Sep 21, 2003
Posts: 26
what is the diff between a new operator and using class.forname()
what object does class.forname() return during loading a driver
please some one help me in buiding these concepts in JDBC suggest some books also ....
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1141

I suggest starting with the JDBC Trail in the Java Tutorial, followed by the JDBC Getting Started Guide, followed by the documentation for the JDBC driver you are using (which, hopefully, is not the JDBC-ODBC bridge driver that comes with the J2SE SDK).
Good Luck,
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
You can do a "Class.forName(...)" or a "new ..." and both will work. I think the first is the preferred method because it saves the initial overhead of actually instantiating an object.
If you look at the documentation for the "java.sql.Driver" interface it states that

When a Driver class is loaded, it should create an instance of itself and register it with the DriverManager. This means that a user can load and register a driver by calling

Doing a "Class.forName()" causes the ClassLoader to find the driver class and load it into the JVM. The tack taken by many [most? all?] drivers is to include a "static { ... }" block of code that will register an instance of the driver with the "DriverManager" class. So all of that work is done "automatically" by the driver class itself.
The "Class.forName()" call actually returns an object of type "java.lang.Class", so you could do:
Class myDriver = Class.forName("foo.bah.Driver");
But since you don't really need to use "myDriver" for anything, most people leave off the left-hand side assignment and just do a "Class.forName(...)";
The links that Avi provided are a good place to get started. Good luck!
I agree. Here's the link:
subject: Loading a driver
jQuery in Action, 3rd edition